Unity 实现出生点(Spawn Points)的Scriptable封装

9 篇文章 0 订阅
4 篇文章 0 订阅

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,同样随机分配点


  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值