『完整代码』坐骑召唤

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

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

相关推荐
夜莺悠吟1 小时前
关于对 C# 中 ImplicitUsings,GlobalUsings 的讨论
c#·.net
-银雾鸢尾-8 小时前
C#中的拓展方法
开发语言·c#
EIP低代码平台20 小时前
EIP低代码平台-系统参数配置详解|落地三级等保+高灵活私有化部署方案
低代码·c#·权限·工作流·netcore
王莎莎-MinerU21 小时前
MCP 解决的是工具接入,科研 Agent 还缺的是科学证据接口标准化
开发语言·网络·人工智能·深度学习·pdf·c#·php
-银雾鸢尾-1 天前
C#中Object类内的方法
开发语言·c#
tiankong12131 天前
如何拓展多态下的子类
设计模式·c#
geats人山人海1 天前
c# 第九章 record
开发语言·c#
-银雾鸢尾-1 天前
C#中的抽象类与抽象方法
开发语言·c#
影寂ldy1 天前
C# Task 进阶:WaitAll / WaitAny / WhenAll / WhenAny
开发语言·c#