[Unity] 코루틴으로 이동하는것 해볼것
APP 2019. 11. 13. 18:50
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;
}
'APP' 카테고리의 다른 글
[Unity] Coroutine 사용해서 공격 애니메이션 실행시 문제, 공격 타이밍이 맞지 않음 (0) | 2019.11.15 |
---|---|
[Unity] 2019-11-14 코루틴 사용 (0) | 2019.11.14 |
[Unity] 2019-11-13 Study_002 역직렬화(캐릭터,웨폰) 인게임(캐릭터,웨폰 생성) (0) | 2019.11.13 |
[Unity] 2019-11-11 Study002 캐릭터의 이동, 애니메이션, grandchild 찾기 (0) | 2019.11.12 |
[Unity] 화살표 그리기 3종류 (0) | 2019.11.07 |