『完整代码』坐骑召唤

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

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

创建脚本并编写:CallMount.cs

cs 复制代码
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);
    }
}

绑定脚本

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

end.

相关推荐
geovindu3 小时前
CSharp: 万年历
开发语言·后端·c#·.net
离陌在学C#11 小时前
C# 异步编程:从 async/await 到 Task 实战指南
开发语言·数据库·c#
用户2986985301415 小时前
PDF 转 HTML 全攻略:三种方法轻松搞定网页转换
c#·html·.net
geovindu15 小时前
CSharp: State Pattern
开发语言·后端·c#·.net·状态模式·行为模式
俊昭喜喜里16 小时前
C#中的decimal
开发语言·c#
JustCheng2 天前
Dispacher(1/2)深入解析-详细使用
c#
CoderIsArt2 天前
C#中UI 线程与 Dispatcher
开发语言·ui·c#
呆呆敲代码的小Y3 天前
【游戏开发】Lua 与 C# 交互原理解析
c#·lua·交互·热更新·lua与c#交互·游戏热更新
用户298698530143 天前
PDF 转纯文本(TXT)免费攻略:轻松提取文字内容
人工智能·后端·c#
呆呆敲代码的小Y3 天前
【游戏开发】C# 中的迭代器
java·开发语言·c#·迭代器