『完整代码』坐骑召唤

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

将预制体放入指定文件夹 命名为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.

相关推荐
hhb_6183 小时前
C#高性能异步编程实战与底层原理深度解析
开发语言·c#
beyond谚语4 小时前
反射、特性和依赖注入
c#
Tiger_shl5 小时前
C# 托管对象、非托管对象 讲解
开发语言·c#
LF男男5 小时前
Action- C# 内置的委托类型
java·开发语言·c#
2501_930707789 小时前
使用C#代码在 PowerPoint 中创建组合图表
开发语言·c#·powerpoint
Full Stack Developme11 小时前
Hutool DFA 教程
开发语言·c#
xiaoshuaishuai811 小时前
【无标题】
开发语言·windows·c#
SunnyDays101111 小时前
C# 如何快速比较 Word 文档并显示差异
c#·对比 word 文档·比较 word 文档
LF男男11 小时前
TouchPad(单例)
unity·c#
武藤一雄1 天前
19个核心算法(C#版)
数据结构·windows·算法·c#·排序算法·.net·.netcore