RoleManager.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using FSRole;
  5. using FSBattle;
  6. /*
  7. * 角色管理
  8. * 未来角色的创建和销毁都在这里
  9. */
  10. public class RoleManager : MonoBehaviour {
  11. // 角色预制体
  12. public GameObject rolePrefab;
  13. // 我方位置
  14. public Transform armyLoc;
  15. // 地方位置
  16. public Transform enemyLoc;
  17. // Use this for initialization
  18. void Start () {
  19. RoleAttr value = new RoleAttr();
  20. value.Camp = RoleCamp.ENEMY;
  21. CreateRole(value);
  22. value.Camp = RoleCamp.ARMY;
  23. CreateRole(value);
  24. }
  25. // Update is called once per frame
  26. void Update () {
  27. }
  28. /*
  29. * 创建角色
  30. */
  31. private void CreateRole(RoleAttr value) {
  32. GameObject role = null;
  33. // 判断是哪个阵营的,则放在指定位置下
  34. if (value.Camp == RoleCamp.ARMY) {
  35. role = Instantiate(rolePrefab, armyLoc);
  36. } else {
  37. role = Instantiate(rolePrefab, enemyLoc);
  38. role.transform.localScale = new Vector3(-1, 1, 1);
  39. }
  40. role.transform.localPosition = new Vector3(0, 0, 0);
  41. // 添加属性
  42. role.GetComponent<Role>().OriginAttr = value;
  43. role.GetComponent<Role>().CurrentAttr = value;
  44. // 加载图片资源
  45. Texture2D tex = Resources.Load<Texture2D>("Textures/Roles/hero/airborne01");
  46. Sprite sp = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector3(0.5f, 0.5f));
  47. if (sp != null) {
  48. role.GetComponent<SpriteRenderer>().sprite = sp;
  49. }
  50. // 最后将其加入数组中
  51. BattleFieldManager.Instance.GetRoleArray().Add(role.transform);
  52. }
  53. }