fairygui自定义扩展+两个例子

本文介绍了如何在FairyGUI中通过自定义组件扩展来实现特定功能,包括使用Unity进行整合的例子。通过两个实例详细展示了自定义组件的定义及在Unity中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

近日看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
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值