当前位置:   article > 正文

Unity 编辑器开发(Button)_unity点击button生成物体

unity点击button生成物体

GUILayout.Button

记录使用GUILayout.Button在编辑器绘制Button按钮

创建脚本UIEditor.cs

using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

[CustomEditor(typeof(TestUI))]
public class UIEditor : Editor
{
    public override void OnInspectorGUI()
    {
        DrawDefaultInspector();
        TestUI test = (TestUI)target;
        if (GUILayout.Button("创建对象"))
        {
            test.CreateObject();
            Debug.Log("创建对象成功");
        }

    }
}

创建脚本TestUI.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestUI : MonoBehaviour
{
    GameObject image;
    Vector3 Vec=new Vector3(50,490,0);
    int index = 0;
    Transform parent;
    private void Start()
    {
        image = Resources.Load<GameObject>("Object/image");
        parent= GameObject.Find("Canvas").transform;
    }
    public void CreateObject()
    {
        GameObject img=Instantiate(image,Vec+new Vector3(index*150,0,0),Quaternion.identity);
        img.transform.parent = parent;
        index++;
    }
}


将脚本挂载在场景中,如图所示:
在这里插入图片描述
将物体image制作成预制体,放置于Resource文件夹下的Object文件夹中
在这里插入图片描述
运行,点击按钮,就能直接生成物体
在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/酷酷是懒虫/article/detail/1022352
推荐阅读
相关标签
  

闽ICP备14008679号