0429 데이터 연동으로 해당 프리팹 불러오기

2021. 4. 29. 10:30unity

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

public class Test : MonoBehaviour
{
    public GameObject GaugeGo;
    public Hero hero;
    public UIButton btn;
    public GameObject labelPrefab;
    public UIPanel panel;
    private Dictionary<int, CharacterData> dicCharDatas;
    private Dictionary<int, GameObject> dicPrefabs = new Dictionary<int, GameObject>();



    // Start is called before the first frame update
    void Start()
    {
        this.btn.onClick.Add(new EventDelegate(() =>
        {
            GameObject go = NGUITools.AddChild(this.panel.gameObject, labelPrefab);
            Vector3 overlay = NGUIMath.WorldToLocalPoint(this.hero.hudDamageTrans.position,
                Camera.main,
                UICamera.mainCamera,
                go.transform);

            overlay.z = 0f;
            go.transform.localPosition = overlay;

            var hud = go.GetComponent<HudText>();
            hud.Init(9999);
        }));

        var ta = Resources.Load<TextAsset>("char_data");
        var json = ta.text;
        this.dicCharDatas = JsonConvert.DeserializeObject<CharacterData[]>(json).ToDictionary(x => x.id);

        foreach (var pair in this.dicCharDatas) 
        {
            var go = Resources.Load<GameObject>(pair.Value.prefab_name);
            this.dicPrefabs.Add(pair.Value.id, go);
        }

        this.CreateHero(100);

    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            Debug.DrawRay(ray.origin, ray.direction * 1000, Color.red, 1.0f);


            var hit = Physics2D.Raycast(ray.origin, ray.direction, 1000);
            if (hit)
            {
                Debug.Log(hit.point);
                this.hero.Move(hit.point);
            }
        }
    }
    private void CreateHero(int id) 
    {
        GameObject go = new GameObject();
        var data = this.dicCharDatas[id];
        go.name = data.prefab_name;

        var model = Instantiate<GameObject>(this.dicPrefabs[id]);
        model.transform.SetParent(go.transform);
        model.name = "model";

        this.hero = go.AddComponent<Hero>();
        this.hero.Init(model, data);
    }
}
using UnityEngine;

public class Hero : MonoBehaviour
{
    public Transform hudGaugeTrans;
    public Transform hudDamageTrans;

    public GameObject hero;
    public float moveSpeed = 2f;

    private Vector3 targetPos;
    private bool isMove;
    public Animator anim;

    public GameObject gaugeGo;
    private Coroutine routine;

    private CharacterData data;
    private GameObject model;


    // Start is called before the first frame update
    void Start()
    {
        this.anim.Play("idle");
    }


    // Update is called once per frame
    void Update()
    {
        Vector3 overlay = NGUIMath.WorldToLocalPoint(this.hudGaugeTrans.position, Camera.main, UICamera.mainCamera, gaugeGo.transform);
        overlay.z = 0f;
        gaugeGo.transform.localPosition = overlay;

    }

    public void Move(Vector3 targetPos)
    {
        if (this.routine != null)
        {
            StopCoroutine(this.routine);
        }
        this.routine = StartCoroutine(this.MoveImpl(targetPos));
    }

    private IEnumerator MoveImpl(Vector3 targetPos) 
    {
        this.anim.Play("run");
        if (this.transform.position.x < targetPos.x)
        {
            this.transform.localScale = Vector3.one;
        }
        else
        {
            this.transform.localScale = new Vector3(-1f, 1f, 1f);
        }
        while (true)
        {
            var dir = (targetPos - this.transform.position).normalized;
            this.transform.Translate(dir * moveSpeed * Time.deltaTime);
            var distance = Vector3.Distance(targetPos, this.transform.position);
            if (distance <= 0.1f)
            {
                this.anim.Play("idle");
                break;
            }
            yield return null;
        }
        
    }

    public void Init(GameObject model, CharacterData data) 
    {
        this.data = data;
        this.model = model;
        this.anim = this.model.GetComponent<Animator>();
        this.anim.Play("idle");
        
    }
}
  {
    "id": "100",
    "name": "홍길동",
    "max_hp": "10",
    "damage": "1",
    "move_speed": "1.5",
    "attack_range": "1.5",
    "prefab_name": "birdOrc"
  },
  {
    "id": "101",
    "name": "임꺽정",
    "max_hp": "10",
    "damage": "1",
    "move_speed": "2",
    "attack_range": "1.5",
    "prefab_name": "centaurGeneral"
  }
]