Unity 传送机制的实现

效果

位置存储

锚点设定

传送界面

 传送效果

代码

位置

扫描二维码关注公众号,回复: 14240631 查看本文章
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));
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_43673589/article/details/124561653