fairygui自定义扩展+两个例子

近日看fairygui的几个例子,试着看懂代码,并照着例子再做一遍, 看是不是真的掌握了,并将其步骤等写在这,不然过段时间我可能又会忘记掉了。

关于组件的扩展,官网上有相关的介绍,用组件的扩展可以完成很多内容,而今天我要学的呢,是自定义扩展:


当基础组件、扩展组件都不能满足你的需求时,你可以编写自定义的扩展。使用API UIObjectFactory.setPackageItemExtension

完成定义。例如: 

UIObjectFactory.setPackageItemExtension(UIPackage.getItemURL(“包名“,”组件A”), MyComponent ); 
public class MyComponent extends GComponent 

override protected function constructFromXML(xml:XML):void 

super.constructFromXML(xml); 
//在这里继续你的初始化 


这样就为组件A指定了一个实现类MyComponent 。以后所有组件A创建出来的对象(包括在编辑器里使用的组件A)都为

MyComponent 类型。例如: 
var obj:MyComponent = UIPackage.createObject(“包名“, ”组件A”) as MyComponent ; 
注意:如果组件A只是一个普通的组件,没有定义“扩展”,那么基类是GComponent,如上例所示;如果组件A的扩展是按钮,

那么MyComponent的基类应该为GButton,如果扩展是进度条,那么基类应该为GProgressBar,等等。这个不要弄错。


首先我们来看看效果图



1,在fairygui编辑器里开始编辑。

两个组件,一个为main,一个为Item。

首先在main组建中布局好,背景加上一个列表。在Item组件中添加Item中的内容。

2,导入unity,需要写两个脚本,一个是关于拓展的Item的类型,此处要注意,若是拓展的是button,则基类写button,

没有选择过就是GCompoent,

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


public class mail :GButton
{
	GTextField  time;
	GTextField  mailname;
	Controller got;
	public override void ConstructFromXML(FairyGUI.Utils.XML cxml)          //zhege
	{
		base.ConstructFromXML(cxml);

		time= this.GetChild("time").asTextField;
		mailname = this.GetChild ("title").asTextField;
		got = this.GetController ("c1");
	
	}
	public void setTime(string value)
	{
		time.text = value;
	}
	public void setname (string value)
	{
		mailname.text = value;
	}
	public void setgot(int value)
	{
		got.selectedIndex = value;
		
	}

	
}

然后是关于显示的最主要的代码段
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using FairyGUI;




public class Extetion : MonoBehaviour {


	GComponent main;
	GList list;
	// Use this for initialization
	void Start () {
		UIPackage.AddPackage("ui/Extension2");
		UIObjectFactory.SetPackageItemExtension (UIPackage.GetItemURL ("Extension2", "mail"), typeof(mail));  //


		main = UIPackage.CreateObject ("Extension2", "main").asCom;
		list = main.GetChild ("n4").asList;


		for (int i = 0; i < 5; i++) {
			mail item = (mail)list.AddItemFromPool();        
			item.setTime ("2017,8,10");
			item.setname ("ayou");
			item.setgot (i % 2);
		}
		list.EnsureBoundsCorrect ();


		GRoot.inst.AddChild (main);
	}
	
	// Update is called once per frame
	void Update () {
		
	}
}

这样就大功告成了!





第二个例子:效果图;


这个例子还将unity的粒子系统加入到了ui之中,在fairygui中布局好之后,用一个空的图形占位,在unity中再将例子系统

放入到这个占位的图形中。

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

public class fire : GComponent 
{
	public override void ConstructFromXML(FairyGUI.Utils.XML cxml)
	{
		base.ConstructFromXML (cxml);

		GGraph graph = this.GetChild ("fire").asGraph;

		Object prefab = Resources.Load ("Flame");
		GameObject go = (GameObject)Object.Instantiate (prefab);
		graph.SetNativeObject (new GoWrapper (go));
	}

}

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

public class Particles2 : MonoBehaviour {

	GComponent main;
	// Use this for initialization
	void Start () {
		UIPackage.AddPackage ("ui/Particles2");
		UIObjectFactory.SetPackageItemExtension (UIPackage.GetItemURL ("Particles2", "fire"), typeof(fire));
		main = UIPackage.CreateObject ("Particles2", "main").asCom;
		GRoot.inst.AddChild (main);
		main.GetChild ("n0").draggable = true;
		main.GetChild ("n1").draggable = true;


		
		
	}
	
	// Update is called once per frame
	void Update () {
		
	}
}




  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
FairyGUI 是一款强大的 UI 编辑器和 UI 框架,其中的遮罩功能可以让我们在 UI 中实现一些非常有趣的效果。在 FairyGUI 中,我们可以使用自定义图形作为遮罩,这篇文章将会介绍如何实现自定义遮罩。 1. 创建自定义遮罩 首先,在 FairyGUI 编辑器中创建一个 UI 节点,并将其属性中的 Mask Type 设置为 Image,这将使该节点成为一个遮罩层。然后,为该节点添加一个 Image 组件,并将其作为遮罩的图像。 接下来,我们需要编写脚本来指定遮罩的形状。我们可以创建一个自定义脚本,继承自 FairyGUI 的 IMask 接口,并实现其中的一个方法:OnPopulateMesh。在这个方法中,我们需要创建一个 Mesh 对象,并设置其顶点和三角形,以定义遮罩的形状。 下面是一个简单的例子: ```csharp using UnityEngine; using FairyGUI; public class CustomMask : MonoBehaviour, IMask { public void OnPopulateMesh(VertexHelper vertexHelper) { Mesh mesh = new Mesh(); Vector3[] vertices = new Vector3[4]; int[] triangles = new int[6]; float width = 200f; float height = 100f; vertices[0] = new Vector3(0f, 0f); vertices[1] = new Vector3(0f, height); vertices[2] = new Vector3(width, height); vertices[3] = new Vector3(width, 0f); triangles[0] = 0; triangles[1] = 1; triangles[2] = 2; triangles[3] = 0; triangles[4] = 2; triangles[5] = 3; mesh.vertices = vertices; mesh.triangles = triangles; vertexHelper.AddUIVertexStream(new List<UIVertex>(), mesh); } } ``` 在这个例子中,我们创建了一个矩形的遮罩,并将其顶点和三角形添加到了 Mesh 对象中。然后,我们使用 VertexHelper 对象将这个 Mesh 对象传递给遮罩层,以便在 UI 中显示出来。 2. 将自定义脚本添加到遮罩层 在脚本编写完成后,我们需要将其添加到遮罩层中。在 FairyGUI 编辑器中,我们可以选择遮罩层节点,并在属性中的 Mask 中选择 Custom Mask。然后,在 Custom Mask 中选择我们刚才编写的自定义脚本。 3. 测试效果 现在,我们可以在场景中测试我们的自定义遮罩了。我们可以在遮罩层的子节点中添加一些 UI 元素,并在运行时观察遮罩的效果。如果一切正常,我们应该能够看到 UI 元素被遮罩的效果。 总结 FairyGUI自定义遮罩功能非常强大,可以让我们在 UI 中实现各种有趣的效果。通过编写自定义脚本,我们可以创建各种形状的遮罩,并将其应用到 UI 中。希望这篇文章能够帮助你理解 FairyGUI自定义遮罩功能,以及如何使用它来实现自己的 UI 效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值