经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » 游戏设计 » 查看文章
unity小地图制作___按比例尺图标布局
来源:cnblogs  作者:小辉歌  时间:2019/2/26 9:22:07  对本文有异议

1.

2.这里小地图显示的范围为整个空间区域,而不是单独的相机渲染区域

3.

4.

5.

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. //图标与对应的物体的映射关系
  6. public class MinimapCell
  7. {
  8. public Transform icon; //在小地图上显示的图标
  9. public Transform referObj; //图标对应的物体
  10. public Transform relativeObj;//地图上标杆物体
  11. public float scaleRealToUI;
  12. RectTransform rectTrans;
  13. public MinimapCell(Transform _icon,Transform _referObj,Transform _relativeObj,float _scaleRealToUI)
  14. {
  15. icon = _icon;//小地图中的图标
  16. referObj = _referObj;//小地图表示的在场景中的物体
  17. relativeObj = _relativeObj;//参考物体,设此物体为坐标计算的中心,从而计算出场景中物体的相对位置
  18. scaleRealToUI = _scaleRealToUI;//比例尺
  19. }
  20. //同步位置
  21. public void Synchro()
  22. {
  23. if (icon && referObj&&relativeObj)
  24. {
  25. rectTrans = icon.GetComponent<RectTransform>();
  26. rectTrans.anchoredPosition3D = -(referObj.position - relativeObj.position)*scaleRealToUI;//根据比例尺设置图标和地图中角色的映射位置
  27. }else if (referObj == null || referObj.gameObject.activeInHierarchy == false)
  28. {//如果角色为空(即死亡),那么隐藏该图标,可用于下一个角色,这里起到了对象池的作用
  29. icon.gameObject.SetActive(false);
  30. }
  31. }
  32. }
  33. //原理:根据世界的宽高比例设置地图UI的宽高大小(其中宽度固定,根据比例得出高度)以及计算出比例尺
  34. //建立列表cells,用于存储当前图标和对应的物体的信息
  35. //如果场景中有需要显示在小地图上的物体,将标杆物体和比例尺赋予图标单元;
  36. //首先查看列表中是否有可用的cell(即该cell为激活状态,表示正对应世界中某个物体),如果有,则激活图标,设置对应物体,如果没有,则生成一个新的cell
  37. //在update中实时调用每个cell的同步函数,实时同步位置
  38. //如果某个物体需要在小地图中表示,那么该物体必须带有组件MiniMapFit组件,改组件用于访问地图,设置同步图标
  39. public class MiniMap : MonoBehaviour
  40. {
  41. public static MiniMap instance;
  42. public Transform relativeObj;//场景中的标杆物体,标杆物体放在世界区域的右上角,并且相对的,小地图的中心点在小地图的右上角,从而对应映射关系
  43. public GameObject cellPrefab;//图标物体的预制物体
  44. public List<MinimapCell> cells;//图标单元列表
  45. public Rect worldSize;//世界区域的真实大小(定为在场景区域中玩家可以移动的区域大小)
  46. RectTransform rectTrans;
  47. float scaleRealToUI;//比例尺
  48. // Start is called before the first frame update
  49. void Awake()
  50. {
  51. rectTrans = GetComponent<RectTransform>();
  52. Debug.Log(rectTrans.position+" "+rectTrans.anchoredPosition3D+" "+rectTrans.rect);
  53. scaleRealToUI = rectTrans.rect.x / worldSize.x;//计算出比例尺
  54. rectTrans.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, -worldSize.y* scaleRealToUI);//设置地图UI的高度
  55. cells = new List<MinimapCell>();
  56. instance = this;
  57. }
  58. // Update is called once per frame
  59. void Update()
  60. {
  61. //实时同步结点
  62. foreach (MinimapCell miniCell in cells)
  63. {
  64. miniCell.Synchro();
  65. }
  66. }
  67. //当增加图标示意对应的角色
  68. public void AddCell(Transform _referObj)
  69. {
  70. bool flag = false;//标记是否查找成功
  71. foreach(MinimapCell miniCell in cells)//查看链表中是否有可用的单元
  72. {
  73. if (miniCell.icon.gameObject.activeInHierarchy == false)
  74. {
  75. miniCell.referObj = _referObj;
  76. miniCell.icon.gameObject.SetActive(true);
  77. flag = true;
  78. break;
  79. }
  80. }
  81. if (!flag)//如果链表中没有空余的结点,那么新增一个结点,用于显示角色位置信息
  82. {
  83. Transform trans = Instantiate(cellPrefab, transform).transform;
  84. MinimapCell cell = new MinimapCell(trans,_referObj,relativeObj,scaleRealToUI);
  85. cells.Add(cell);
  86. }
  87. }
  88. private void OnDrawGizmos()
  89. {
  90. Gizmos.color = Color.green;
  91. if(relativeObj)Gizmos.DrawWireCube(relativeObj.position- new Vector3(worldSize.x / 2, worldSize.y / 2), new Vector3(worldSize.x, worldSize.y));
  92. }
  93. }

6.

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. //此类绑定在角色身上,在场景中生成角色时,可以在小地图中生成对应的图标
  5. public class MiniMapFit : MonoBehaviour
  6. {
  7. // Start is called before the first frame update
  8. void Start()
  9. {
  10. if (MiniMap.instance)//访问小地图,生成图标
  11. {
  12. MiniMap.instance.AddCell(transform);
  13. }
  14. }
  15. }

 

原文链接:http://www.cnblogs.com/xiaoahui/p/10434872.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号