Unity 传送机制的实现

效果

位置存储

锚点设定

传送界面

 传送效果

代码

位置

using System.Collections.Generic;
using UnityEngine;

[CreateAssetMenu]
public class Locations : ScriptableObject
{
    public List<Location> locations;
}

位置保存工具

using UnityEditor;
using UnityEngine;

public class LocationSavingTool : Editor
{
    public static Locations locationsAsset = Resources.Load<Locations>("Configurations/Locations");

    [MenuItem("CONTEXT/Transform/保存位置")]
    static void SaveLocationToAsset()
    {
        GameObject thisObject = Selection.activeGameObject;
        locationsAsset.locations.Add(new Location(thisObject.name, thisObject.transform.position));
    }
}

位置管理器

using UnityEngine;

public class LocationsManager : Singleton<LocationsManager>
{
    public Locations locationsAsset = Resources.Load<Locations>("Configurations/Locations");

    public Vector3 GetLocationByName(string name)
    {
        foreach (var i in locationsAsset.locations)
        {
            if (i.name == name)
            {
                return i.location;
            }
        }
        return Vector3.zero;
    }
}

传送

using UnityEngine;

public class Teleport : MonoBehaviour
{
    void Start()
    {
        NotificationCenter.DefaultCenter().AddObserver(this, "PlayerTeleport");
    }

    public void PlayerTeleport(Notification notification)
    {
        GameObject player = GameObject.FindGameObjectWithTag("Player");
        player.transform.position = (Vector3)notification.data;
    }
}

传送锚点

using UnityEngine;

public class TeleportPoint : MonoBehaviour
{
    public string name;

    public void OnClicked()
    {
        NotificationCenter.DefaultCenter().PostNotification(this, "PlayerTeleport", LocationsManager.Instance.GetLocationByName(name));
    }
}

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值