[Unity] 코루틴으로 이동하는것 해볼것

APP 2019. 11. 13. 18:50

character_data.json
0.00MB
stage_data.json
0.00MB

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Newtonsoft.Json;

public class App : MonoBehaviour
{
    public Button btnStage;
    public Button btnHero;
    public Button btnMonster;
    public Button btnAttack;
    public Dictionary<int, CharacterData> dicCharacterData;
    public Dictionary<int, StageData> dicStageData;
    Dictionary<int, GameObject> dicPrefab;
    private Stage stage;
    private Dictionary<int,Character> dicCharacter;
    private Vector3 heroPos;
    private float speed = 0.5f;
    private void Awake()
    {

        this.dicCharacterData = new Dictionary<int, CharacterData>();
        this.dicStageData = new Dictionary<int, StageData>();
        this.dicPrefab = new Dictionary<int, GameObject>();
        this.dicCharacter = new Dictionary<int, Character>();

        CharacterData[] arrCharacterData = JsonConvert.DeserializeObject<CharacterData[]>(Resources.Load("Data/character_data").text);
        
        foreach(CharacterData data in arrCharacterData)
        {
            this.dicCharacterData.Add(data.id, data);
        }

        StageData[] arrStageData = JsonConvert.DeserializeObject<StageData[]>(Resources.Load("Data/stage_data").text);
        foreach(StageData data in arrStageData)
        {
            this.dicStageData.Add(data.id, data);
        }

        LoadCharacterPrefabs();
        LoadStagePrefabs();
    }
    public void LoadCharacterPrefabs()
    {
        foreach(KeyValuePair<int,CharacterData> keyValuePair in dicCharacterData)
        {
           this.dicPrefab.Add(keyValuePair.Key, Resources.Load($"Prefabs/{keyValuePair.Value.prefab_name}"));
        }
    }
    public void LoadStagePrefabs()
    {
        foreach (KeyValuePair<int, StageData> keyValuePair in dicStageData)
        {
            this.dicPrefab.Add(keyValuePair.Key, Resources.Load($"Prefabs/{keyValuePair.Value.prefab_name}"));
        }
    }

    // Start is called before the first frame update
    void Start()
    {

        btnStage.onClick.AddListener(()=>{
            Debug.Log("Stage버튼");
            this.CreateStage(dicStageData[1000]);
        });

        btnHero.onClick.AddListener(() => {
            Debug.Log("Hero버튼");
            this.CreateCharacter(dicCharacterData[100]);
            dicCharacter[100].transform.SetParent(GameUtils.SearchHierarchyForBone(this.stage.transform,"HeroInitPos"), false);
        });

        btnMonster.onClick.AddListener(() => {
            Debug.Log("Monster버튼");
            this.CreateCharacter(dicCharacterData[200]);
            dicCharacter[200].transform.SetParent(GameUtils.SearchHierarchyForBone(this.stage.transform, "MonsterInitPos"), false);
        });

        btnAttack.onClick.AddListener(() =>
        {
            Debug.Log("Attack버튼");
        });



    }
    public void CreateStage(StageData stageData)
    {
        GameObject go = new GameObject(stageData.name);
        Stage stage = go.AddComponent();
        GameObject model = Instantiate(dicPrefab[stageData.id]);
        model.transform.SetParent(stage.transform, false);
        model.transform.localPosition = Vector3.zero;
        this.stage = stage;
    }
    public void CreateCharacter(CharacterData characterData) where T : Character
    {
        GameObject go = new GameObject(characterData.name);
        T character = go.AddComponent();
        GameObject model = Instantiate(dicPrefab[characterData.id]);
        model.transform.SetParent(character.transform, false);
        model.transform.localPosition = Vector3.zero;
        dicCharacter.Add(characterData.id, character);
    }
    IEnumerator MoveCharacter()
    {
        
        while(true)
        {
            yield return new WaitForSeconds(0.02f);
            this.dicCharacter[100].transform.
        }
    }
    // Update is called once per frame
    void Update()
    {
      
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Character : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        
    }
    public void InitPos(GameObject InitPos)
    {
        this.transform.SetParent(InitPos.transform);
    }
    // Update is called once per frame
    void Update()
    {
        
    }
}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CharacterData
{
    //int string  int int       float       string
    //id  name    hp  damage    atk_range   prefab_name
    public int id;
    public string name;
    public int hp;
    public int damage;
    public float atk_range;
    public string prefab_name;
}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CharacterInfo
{
    public int id;
    public string name;
    public int hp;
}

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public static class GameUtils
{
    //HOW TO USE
    //var cube = this.transform.FirstChildOrDefault(x => x.name == "deeply_nested_cube");
    public static Transform FirstChildOrDefault(this Transform parent, Func<Transform, bool> query)
    {
        if (parent.childCount == 0)
        {
            return null;
        }

        Transform result = null;
        for (int i = 0; i < parent.childCount; i++)
        {
            var child = parent.GetChild(i);
            if (query(child))
            {
                return child;
            }
            result = FirstChildOrDefault(child, query);
        }

        return result;
    }


    public static Transform SearchHierarchyForBone(Transform current, string name)
    {
        //sb.Append(current.name + "\n");

        // check if the current bone is the bone we're looking for, if so return it
        if (current.name == name)
            return current;

        // search through child bones for the bone we're looking for
        for (int i = 0; i < current.childCount; ++i)
        {
            // the recursive step; repeat the search one step deeper in the hierarchy
            var child = current.GetChild(i);

            Transform found = SearchHierarchyForBone(child, name);

            // a transform was returned by the search above that is not null,
            // it must be the bone we're looking for
            if (found != null)
                return found;
        }

        // bone with name was not found
        return null;
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Hero : Character
{
    // Start is called before the first frame update
    void Start()
    {
        
    }
   
    // Update is called once per frame
    void Update()
    {
        
    }
}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Monster : Character
{
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Stage : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class StageData 
{
    //int string string
    //id  name prefab_name
    public int id;
    public string name;
    public string prefab_name;
}




: