四个类完成,前提是 资源得名字配合
UI按钮点击响应类
using UnityEngine;
using System.Collections; public class ButtonClickHandler : MonoBehaviour {
//处理业务控制类
private SkinController skinController = null; //按钮执行相应业务得向导
public void OnButtonClickWizard()
{
var arr= UIButton.current.name.Split(' ');
var function = arr[0];
var goName=arr[1];
switch (function)
{
case "Character":
skinController.ChangeCharacter(goName);
break;
case "Clother":
skinController.ChangeClother(goName);
break;
case "Weapons":
skinController.ChangeWeapons(goName);
break;
case "Animation":
skinController.ChangeAnimation(goName);
break;
}
}
/// <summary>
/// 初始化为所有Button注册点击后得响应方法
/// </summary>
void Start () {
skinController = FindObjectOfType<SkinController>();
for (int i = 0; i < transform.childCount; i++)
{
transform.GetChild(i).GetComponent<UIButton>().onClick.Add(new EventDelegate(OnButtonClickWizard));//NGUI
}
} }
加载资源工厂类
using UnityEngine;
using System.Collections.Generic; /// <summary>
/// 创建动态加载资源
/// </summary>
public class CharacterFactory {
//各类资源得位置
private string CharacterPath = "Prefabs/mon_";
private string ClotherPath = "Clother/";
private string WeaponPath = "Weapon/wp_";
//资源对象内存缓存
private Dictionary<string, Object> GoCache; private static CharacterFactory instance;
public static CharacterFactory Instance
{
get
{
if(instance==null)
instance = new CharacterFactory();
return instance;
}
} public CharacterFactory()
{
GoCache = new Dictionary<string, Object>();
}
public GameObject CreateCharacter(string characterName)
{
return LoadResource<GameObject>(CharacterPath+ characterName);
}
public GameObject CreateWeapon(string weaponName)
{
return LoadResource<GameObject>(WeaponPath + weaponName);
}
public Texture CreateClother(string clotherName)
{
return LoadResource<Texture>(ClotherPath + clotherName);
} private T LoadResource<T>(string resourceName)
where T :Object
{
//检测是否已创建该资源
if(GoCache.ContainsKey(resourceName))
{
T go = (T)Resources.Load(resourceName); if(!(go is Texture))//如果不是贴图就运行下面
{
go = (T)GameObject.Instantiate(go);
go.name = go.name.Replace("(Clone)", "");
GoCache.Add(resourceName, go);
}
}
//有直接返回
return (T)GoCache[resourceName];
}
}
业务处理控制类
using UnityEngine;
using System.Collections;
using System; /// <summary>
/// 业务处理控制类
/// </summary>
public class SkinController : MonoBehaviour { public GameObject currentCharacter = null;
public GameObject currentWeapon = null; /// <summary>
/// 换角色
/// </summary>
/// <param name="goName"></param>
public void ChangeCharacter(string goName)
{
var character = CharacterFactory.Instance.CreateCharacter(goName);
//判断当前角色是否为空, 或当前角色已展示
if (currentCharacter != null && currentCharacter.name != character.name)
currentCharacter.SetActive(false);
character.transform.position = transform.position;
character.transform.rotation = transform.rotation;
this.currentCharacter = character;
this.currentCharacter.SetActive(true);
}
/// <summary>
/// 换服装
/// </summary>
/// <param name="goName"></param>
public void ChangeClother(string goName)
{
if(currentCharacter!=null)
{
var clother= CharacterFactory.Instance.CreateClother(currentCharacter.name+"_"+goName);
var chName = currentCharacter.name;
currentCharacter.transform.FindChild(chName).GetComponent<Renderer>().material.mainTexture = clother;
}
}
/// <summary>
/// 换武器
/// </summary>
/// <param name="goName"></param>
public void ChangeWeapons(string goName)
{
if (currentCharacter != null)
{
var weapon = CharacterFactory.Instance.CreateWeapon(goName);
if (currentWeapon != null && currentWeapon.name != weapon.name)
currentWeapon.SetActive(false);
GameObject wpHand = GlobalTool.FindChild(currentCharacter.transform, "wphand"); currentWeapon = weapon;
currentWeapon.transform.parent = wpHand.transform;
currentWeapon.transform.position = wpHand.transform.position;
currentWeapon.transform.rotation = wpHand.transform.rotation;
currentWeapon.transform.localScale = new Vector3(1, 1, 1);
currentWeapon.SetActive(true);
} }
/// <summary>
/// 播放动画
/// </summary>
/// <param name="goName"></param>
public void ChangeAnimation(string goName)
{ currentCharacter.GetComponent<Animation>().Play(goName);
}
}
查找角色上子物体优化算法类
using UnityEngine;
using System.Collections; public class GlobalTool { public static GameObject FindChild(Transform trans,string goName)
{
var child=trans.FindChild(goName);
if(child!= null)
return child.gameObject;
GameObject go = null;
for (int i = 0; i < trans.childCount; i++)
{
child= trans.GetChild(i);
go= FindChild(child, goName);
if(go!=null)
{
return go;
}
}
return null;
}
}