Unity 实现出生点(Spawn Points)的Scriptable封装
来源:互联网 发布:旅行社软件管理 编辑:程序博客网 时间:2024/05/20 13:05
Unity 实现出生点(Spawn Points)的Scriptable封装
详细代码见 Github : Points
后来又重新封装了Point。见这里:Unity 自制工具:Point。方便标记出生点或巡逻点等功能。
传统设置游戏出生点是在场景中创建一堆GameObject并使用其Transform作为出生点位置。这是有点大材小用了。如果能封装出来到一个列表里面(Scriptable Object),需要使用的时候调用这个列表就不仅让层级列表少了一堆多余的GameObject,还可以实现序列化存储。当然不只是用于出生点,也可以作为其他用途如下面的坦克AI的巡逻点。
点对象(Point)
using UnityEngine;[System.Serializable]public class Point{ [HideInInspector] public bool enable = true; public Vector3 position; public Vector3 rotation; public Quaternion Rotation { get { return Quaternion.Euler(rotation); } }}
说明:
- enable,通过这个变量可以判断当前点是否已经被用过。
- position,点的位置。
- rotation,旋转角度(Vector3)。
- Rotation,获取旋转角度的四元组类型值(Quaternion)。
点对象列表(PointList)
代码挑重点:获取随机点
/// <summary> /// 获取随机点 /// </summary> /// <param name="allowDisable">是否允许获取使用过的点</param> /// <param name="isDifferent">是否是不同于当前的点</param> /// <returns>返回获取到的点</returns> public Point GetRandomPoint(bool allowDisable = true, bool isDifferent = true) { if (IsEmpty()) return null; if (allowDisable) //允许获取使用过的点 { if (isDifferent) //要求是不同于上一次选择的点 return SetCurrentPointByIndex(GetRandomDifferenceIndex(currentIndex, 0, pointList.Count)); return SetCurrentPointByIndex(Random.Range(0, pointList.Count)); //随机获取点列表任意点 } //下面获取未使用过的点(Point.enable == falase) List<int> enablePoints = GetEnablePointsIndex(isDifferent); // 获取有效的点索引列表 if (enablePoints == null) // 不存在返回null return null; currentIndex = enablePoints[Random.Range(0, enablePoints.Count)]; // 再从有效点索引列表中随机获取有效点索引 pointList[currentIndex].enable = false; // 设置点无效(已经使用过) return pointList[currentIndex]; } /// <summary> /// 设置当前索引值,并返回对应的点 /// </summary> /// <param name="index">索引值</param> /// <returns>返回该索引对应的点</returns> private Point SetCurrentPointByIndex(int index) { currentIndex = index; return pointList[index]; } /// <summary> /// 获取在min到max范围内不同于current值的随机数 /// </summary> /// <param name="current">当前值</param> /// <param name="min">最小值</param> /// <param name="max">最大值</param> /// <returns>失败返回-1</returns> public int GetRandomDifferenceIndex(int current, int min, int max) { if (current == min && min == max) return -1; int index; do { index = Random.Range(min, max); } while (index == current); //死循环获取不等于current值的随机数 return index; } /// <summary> /// 获取未使用过的所有点 /// </summary> /// <param name="isDifferent">是否需要不同于当前的点</param> /// <returns></returns> private List<int> GetEnablePointsIndex(bool isDifferent = false) { List<int> enablePoints = new List<int>(); // 存放点索引 for (int i = 0; i < pointList.Count; i++) { if (!pointList[i].enable || (isDifferent && i == currentIndex)) continue; enablePoints.Add(i); } if (enablePoints.Count == 0) { Debug.LogError("There are no enable Point can use!"); return null; } return enablePoints; }
结果如下:(代码见文章开头Github链接)
创建点列表(Create -> Game Configure -> Point List)
说明:
- Point Color,点颜色
- Show Point Color,是否显示点颜色
- Show Axis,是否显示点的三个坐标
- Point List,点列表
对应场景显示如下
说明:
- 对绿色点对应上图的列表,通过坐标可以知道点的方向。
- 蓝色点作为下图旗帜的出生点。
- 紫色点作为AI的巡逻点。
回合1,坦克随机分配到绿色点位置,并设置旋转角度
回合2,同样随机分配点
阅读全文
0 0
- Unity 实现出生点(Spawn Points)的Scriptable封装
- Unity中的序列化Scriptable
- 如何实现unity函数的再次封装?
- (39)玩家出生点Actor
- 实现spawn-fcgi的守护监控功能
- 实现spawn-fcgi的守护监控功能
- Unity 自制工具:Point。方便标记出生点或巡逻点等功能。
- Unity消息事件的封装(转)
- unity-unet-动态spawn player
- 固定主角出生点——对象层的使用
- Unicode Code Points (Unicode代码点)
- NTFS重解析点(Reparse Points)
- Unity之一天一个技术点(十二)---指南针的实现
- linux expect, spawn 用expect实现自动登录的脚本
- 在Unity中实现点云的效果
- Windows phone 7 里silverlight的点与像素 (Points and Pixels in silverlight)
- LeetCode 149. Max Points on a Line(直线上的点)
- ACE_Message_Queue和spawn实现(生产者/消费者)(V2.00)
- 使用Flume将MySQL表数据实时抽取到hadoop
- 关于ajax中contenttype=false的问题
- 基于RTP的H264视频数据打包解包类
- Linux--记录使用Centos 7 的坑点
- 菜鸟柳--23种设计模式初学记录笔记(二)装饰者设计模式
- Unity 实现出生点(Spawn Points)的Scriptable封装
- 高通平台:USB充电
- js注意的细节
- LeetCode 3. Longest Substring Without Repeating Characters题解python
- Java学习 String 类
- QTP11的下载地址和破解教程
- 1像素Activity进程保活
- jQuery使用最广泛的javascript函数库
- Java线程:Callable和Future