FairyGUI-unity icon indicating copy to clipboard operation
FairyGUI-unity copied to clipboard

Attribute Suggestion

Open netpyoung opened this issue 8 years ago • 4 comments

only GetChild function, It is hard to managemet code. so I suggested using attribute

before

GButton item = list1.AddItemFromPool().asButton;
item.GetChild("t0").text = "" + (i+1);
item.GetChild("t1").text = testNames[i];
item.GetChild("t2").asTextField.color = testColor[UnityEngine.Random.Range(0, 4)];
item.GetChild("star").asProgress.value = (int)((float)UnityEngine.Random.Range(1, 4) / 3f * 100);

after

public class AListItem : BaseGComponentHelper
{
	[GChild]
	FairyGUI.GTextField t0 = null;

	[GChild]
	FairyGUI.GTextField t1 = null;

	[GChild]
	FairyGUI.GTextField t2 = null;

	[GChild("star")]
	GProgressBar bar = null;

	public AListItem(GComponent comp) : base(comp)
	{
	}

	public void Init(string txt, string name, Color color, float progress)
	{
		t0.text = txt;
		t1.text = name;
		t2.color = color;
		bar.value = progress;
	}
}

var txt = "" + (i + 1); ;
var name = testNames[i];
var color = testColor[UnityEngine.Random.Range(0, 4)];
var progress = (int)((float)UnityEngine.Random.Range(1, 4) / 3f * 100);

var item = list1.AddItemFromPool().As<AListItem>();
item.Init(txt, name, color, progress);

implementation example i know it invoke reflection function effect on performance. but sometimes it will be deserved.

using UnityEngine;

using FairyGUI;
using System;
using System.Reflection;
using System.Linq;

public static class GComponentExtension
{
	public static T As<T>(this GObject obj) where T : BaseGComponentHelper
	{
		return (T)Activator.CreateInstance(typeof(T), obj.asCom);
	}
}

public class BaseGComponentHelper
{
	public BaseGComponentHelper(GComponent comp)
	{
		LoadComponents(comp);
	}

	public void LoadComponents(GComponent comp)
	{
		var behaviour_type = this.GetType();
		var path_type = typeof(GChildAttribute);

		var fields = behaviour_type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).Where(m => m.GetCustomAttributes(path_type, true).Length == 1);

		foreach(var field in fields)
		{
			var attribute = field.GetCustomAttributes(path_type, true)[0] as GChildAttribute;
			var path = (attribute.IsSelf) ? field.Name : attribute.path;

			var gobject = comp.GetChild(path);
			if(gobject == null)
			{
				Debug.LogError(string.Format("can't find gobject in {0}", path));

				continue;
			}

			field.SetValue(this, gobject);
		}
	}
}

public class GChildAttribute : Attribute
{
	public readonly string path = null;

	public bool IsSelf
	{
		get { return this.path == null; }
	}

	public GChildAttribute(string path)
	{
		this.path = path;
	}

	public GChildAttribute()
	{
		this.path = null;
	}
}

netpyoung avatar Nov 29 '16 01:11 netpyoung

Are these reflection codes to be a problem in IOS/il2cpp? I am not sure and I think they need to carefully tested.

fairygui avatar Nov 30 '16 03:11 fairygui

I don't know about belew code is work.(I didn't tested) return (T)Activator.CreateInstance(typeof(T), obj.asCom);

but another code is work on iOS/il2cpp. I will check it.

netpyoung avatar Dec 01 '16 01:12 netpyoung

i checked that code with iOS/il2cpp. tested environment : Unity 5.5.0f3 - Release - .NET 2.0 Subset

it is worked.

netpyoung avatar Dec 04 '16 12:12 netpyoung

OK, thanks for your information.

fairygui avatar Dec 05 '16 09:12 fairygui