创建一个按钮 作为召唤/消失坐骑的开关

『完整代码』坐骑召唤-LMLPHP

将预制体放入指定文件夹 命名为Mount01

『完整代码』坐骑召唤-LMLPHP

创建脚本并编写:CallMount.cs

『完整代码』坐骑召唤-LMLPHP

using UnityEngine;
using UnityEngine.UI;
public class CallMount : MonoBehaviour{
    public Button callBtn;
    GameObject mountPrefab;
    GameObject mountInstance;
    GameObject player;
    void Start(){
        mountPrefab = Resources.Load<GameObject>("Prefabs/Mounts/Mount01");
        callBtn.onClick.AddListener(OnCallButtonClick);
        player = GameObject.Find("PlayerNormal(Clone)/Player").gameObject;
    }
    public void OnCallButtonClick(){
        if (mountInstance == null){
            mountInstance = Instantiate(mountPrefab,
                player.transform.position + new Vector3(-2f, 0, -1f),
                Quaternion.identity);
        }
        else{
            if (player != null)
                Destroy(mountInstance);
        }
    }
    void OnDestroy(){
        if (callBtn != null)
            callBtn.onClick.RemoveListener(OnCallButtonClick);
    }
}

绑定脚本

『完整代码』坐骑召唤-LMLPHP

运行项目 - 点击召唤可以在主角身旁召唤坐骑 - 再次点击消失

『完整代码』坐骑召唤-LMLPHP

end.

10-23 18:24