一、FairyGUI编辑器中
1.新建按钮、新建组件
编辑器中界面简易设计如下
![](https://i-blog.csdnimg.cn/direct/875f7b1c78174d7f94635c3de29efed0.png)
2.文件-发布设置-发布路径:自己unity项目Resources所在的路径
![](https://i-blog.csdnimg.cn/direct/2b9e4b6c967044aea1bef2564f48bb0a.png)
二、Unity 使用代码展示UI
![](https://i-blog.csdnimg.cn/direct/9120b27c476b4d7ba53e0952e71b35e4.png)
csharp
using FairyGUI;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FguiTest : MonoBehaviour
{
/// <summary>
/// 普通按钮
/// </summary>
private GButton comBtn1;
private GButton comBtn3;
void Start()
{
GRoot.inst.SetContentScaleFactor(1920,1080); //初始化设置分辨率
UIPackage.AddPackage("Panels/PanelMain"); //加载打包好的项目
GComponent component = UIPackage.CreateObject("PanelMain", "Component1").asCom;
//加载对应的组件 通过.asCom(或as GComponent)类型转换将它转换为GComponent组件类型
GRoot.inst.AddChild(component); //把当前组件实例化到UI Panel(GRoot实际上是UI Panel)的下面
comBtn1 = component.GetChild("n1").asButton;
comBtn1.onClick.Add(() =>
{
Debug.Log("按钮1被点击了!!");
});
comBtn3 = component.GetChild("n3").asButton;
comBtn3.onClick.Add(() =>
{
Debug.Log("普通按钮2被点击!!!");
});
}
}
三、Unity 无代码展示UI
在Hierarchy中创建Stage Camera和 UIPanel
![](https://i-blog.csdnimg.cn/direct/1fb976353d8f43518ba74d639d87a9e7.png)
![](https://i-blog.csdnimg.cn/direct/76e503753c0d41cc9c86a2233149ad16.png)
![](https://i-blog.csdnimg.cn/direct/c912702ce50a4e90a6b7f2877f0aef15.png)
四、备注:场景中Main Camera的属性 Culling Mask 中不勾选UI,否则会出现两个相机画面。
![](https://i-blog.csdnimg.cn/direct/61ba074289734bfea6ffaf7febb66684.png)