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++;
}
}
将脚本挂载在场景中,如图所示: ![在这里插入图片描述](https://img-blog.csdnimg.cn/cbb4bf276aef40b8af43f405f62af6c6.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5a6H5a6Z5aW955S35Lq6,size_20,color_FFFFFF,t_70,g_se,x_16) 将物体image制作成预制体,放置于Resource文件夹下的Object文件夹中 ![在这里插入图片描述](https://img-blog.csdnimg.cn/3f2a1fd7f3d94f95b1b469c68a2ea4e2.png) 运行,点击按钮,就能直接生成物体 ![在这里插入图片描述](https://img-blog.csdnimg.cn/d4916f1bfebb4a779d6d3db5ea394919.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5a6H5a6Z5aW955S35Lq6,size_20,color_FFFFFF,t_70,g_se,x_16)
|