『完整代码』坐骑召唤

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

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

相关推荐
JQLvopkk15 分钟前
能用C#开发AI
开发语言·人工智能·c#
游乐码8 小时前
c#类和对象
开发语言·c#
沃码12 小时前
【Lively Wallpaper 】插件开发:LivelyProperties.json 全流程实战教程
c#·json·livelywallpaper·视频壁纸
github.com/starRTC17 小时前
Claude Code中英文系列教程34:再谈Skills
开发语言·c#·ai编程
bugcome_com19 小时前
C# 判断语句详解(if、switch、三目运算符、Null 条件运算符)
c#
霸王•吕布21 小时前
C#-使用OpenTK渲染3D模型
c#·opentk·glcontrol
游乐码21 小时前
c#封装成员变量和成员方法和访问修饰符
开发语言·c#
Never_Satisfied1 天前
在c#中,Jint的AsString()和ToString()的区别
服务器·开发语言·c#
Never_Satisfied1 天前
在c#中,获取文件的大小
java·开发语言·c#
weixin_468466851 天前
PyTorch导出ONNX格式分割模型及在C#中调用预测
人工智能·pytorch·深度学习·c#·跨平台·onnx·语义分割