123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413 |
- using UnityEngine;
- using System.Collections;
- using System;
- using UnityEditor;
- using FairyGUI;
- using System.Collections.Generic;
- using System.Linq;
- namespace GFGGame
- {
- public class SceneController
- {
- public static void UpdateLoginScene(GameObject sceneObj)
- {
- //背景
- Transform tf = sceneObj.transform.Find("Bg");
- SpriteRenderer spr = tf.GetComponent<SpriteRenderer>();
- var resPath = ResPathUtil.GetDressUpPath("jhsy_bg", "jpg");
- Sprite sp = GFGAsset.Load<Sprite>(resPath);
- DressUpUtil.AddAssetReleaser(tf.gameObject, resPath);
- spr.sprite = sp;
- //角色
- DressUpUtil.UpdateBody("ui_loginrole", sceneObj, true, null);
- }
- public static void UpdateMainScene(GameObject sceneObj)
- {
- //背景
- Transform tf = sceneObj.transform.Find("Bg");
- SpriteRenderer spr = tf.GetComponent<SpriteRenderer>();
- var resPath = ResPathUtil.GetDressUpPath("zjm_bg1", "jpg");
- Sprite sp = GFGAsset.Load<Sprite>(resPath);
- DressUpUtil.AddAssetReleaser(tf.gameObject, resPath);
- spr.sprite = sp;
- //角色
- CustomSuitData suitSavedData = CustomSuitDataManager.GetCurrentSuitList();
- List<int> equipDatas = suitSavedData.equipDatas;
- UpdateRole(equipDatas, sceneObj, false, null, true, null, false);
- if (suitSavedData.pic && suitSavedData.suitId > 0)
- {
- SuitCfg suitCfg = SuitCfgArray.Instance.GetCfg(suitSavedData.suitId);
- DressUpUtil.UpdateBody(suitCfg.picRes, sceneObj, !string.IsNullOrEmpty(suitCfg.aniRes), suitCfg.effRes);
- }
- else
- {
- DressUpUtil.UpdateBody(null, sceneObj);
- }
- }
- public static void UpdateRole(int[] equipDatas, GameObject sceneObj, bool needSetMask = false, int[] exceptTypes = null, bool showAni = true, GameObject parentObj = null, bool reset = true)
- {
- if (reset)
- {
- Reset(sceneObj);
- }
- int count = equipDatas.Length;
- for (int i = 0; i < count; i++)
- {
- int id = (int)equipDatas[i];
- if (exceptTypes != null)
- {
- int subType = ItemUtilCS.GetItemSubType(id);
- if (Array.IndexOf(exceptTypes, subType) >= 0)
- {
- continue;
- }
- }
- DressUpUtil.AddItem(id, sceneObj, needSetMask, showAni, parentObj);
- }
- DressUpUtil.UpdateBody(null, sceneObj, false, null, needSetMask, parentObj);
- }
- public static void UpdateRole(List<int> equipDatas, GameObject sceneObj, bool needSetMask = false, int[] exceptTypes = null, bool showAni = true, GameObject parentObj = null, bool reset = true)
- {
- UpdateRole(equipDatas.ToArray(), sceneObj, needSetMask, exceptTypes, showAni, parentObj, reset);
- }
- public static void UpdateDialogBg(string value, GameObject sceneObj)
- {
- Transform tf = sceneObj.transform.Find("Bg");
- SpriteRenderer spr = tf.GetComponent<SpriteRenderer>();
- if (value == "0")
- {
- spr.sprite = null;
- }
- else
- {
- var resPath = ResPathUtil.GetDressUpPath(value, "jpg");
- Sprite sp = GFGAsset.Load<Sprite>(resPath);
- DressUpUtil.AddAssetReleaser(tf.gameObject, resPath);
- spr.sprite = sp;
- }
- }
- public static void UpdateDialogPic(string value, GameObject sceneObj)
- {
- Transform tf = sceneObj.transform.Find("Pic");
- SpriteRenderer spr = tf.GetComponent<SpriteRenderer>();
- if (value == "0")
- {
- spr.sprite = null;
- }
- else
- {
- void UpdateDialogPicAlpha(object param)
- {
- if (spr != null)
- {
- Color c = spr.color;
- if (spr.enabled && c.a < 1f)
- {
- c.a += 0.05f;
- spr.color = c;
- }
- else
- {
- FairyGUI.Timers.inst.Remove(UpdateDialogPicAlpha);
- }
- }
- else
- {
- FairyGUI.Timers.inst.Remove(UpdateDialogPicAlpha);
- }
- }
- var resPath = ResPathUtil.GetNpcPicSPath(value);
- Sprite sp = GFGAsset.Load<Sprite>(resPath);
- DressUpUtil.AddAssetReleaser(tf.gameObject, resPath);
- spr.sprite = sp;
- Color c = spr.color;
- c.a = 0f;
- spr.color = c;
- FairyGUI.Timers.inst.AddUpdate(UpdateDialogPicAlpha);
- }
- }
- public static void UpdateFightTarget(string value, GameObject sceneObj)
- {
- Transform tf = sceneObj.transform.Find("Npc");
- SpriteRenderer spr = tf.GetComponent<SpriteRenderer>();
- var resPath = ResPathUtil.GetNpcPicFPath(value);
- Sprite sp = GFGAsset.Load<Sprite>(resPath);
- DressUpUtil.AddAssetReleaser(tf.gameObject, resPath);
- spr.sprite = sp;
- }
- private static void Reset(GameObject sceneObj)
- {
- //背景层次
- Transform bg = sceneObj.transform.Find("Bg");
- if (bg != null)
- {
- SpriteRenderer spr = bg.GetComponent<SpriteRenderer>();
- if (spr != null)
- {
- ItemTypeCfg typeCfg = ItemTypeCfgArray.Instance.GetCfg(ConstDressUpItemType.BEI_JING);
- spr.sortingOrder = typeCfg.defaultLayer;
- }
- }
- //角色清理
- Transform role = sceneObj.transform.Find("Role");
- if (role != null)
- {
- int childCount = role.childCount;
- for (int i = childCount - 1; i >= 0; --i)
- {
- Transform child = role.GetChild(i);
- if (child.gameObject.name != "Body")
- {
- GameObject.DestroyImmediate(child.gameObject);
- }
- }
- }
- }
- //拍照角色
- public static void UpdatePhotographBody(List<int> equipDatas, GameObject sceneObj, GameObject parentObj)
- {
- SceneController.UpdateRole(equipDatas, sceneObj, false, null, false, parentObj);
- if (EquipDataCache.cacher.IsSuitPic && EquipDataCache.cacher.suitId > 0)
- {
- SuitCfg suitCfg = SuitCfgArray.Instance.GetCfg(EquipDataCache.cacher.suitId);
- DressUpUtil.UpdateBody(suitCfg.picRes, sceneObj, false, null, false, parentObj);
- }
- GameObject gameObject = parentObj.transform.Find("Body").gameObject;
- SceneController.SetBoxCollider2DToGameObject(gameObject);
- }
- //拍照场景添加单个道具
- public static void AddItemToScene(GameObject sceneObj, GameObject parentGameObj, int itemId, int resLayer)
- {
- ItemCfg itemCfg = ItemCfgArray.Instance.GetCfg(itemId);
- parentGameObj.transform.SetParent(sceneObj.transform.Find("Scene"), false);
- DressUpUtil.AddItem(itemId, sceneObj, false, false, parentGameObj, resLayer);
- // if (parentGameObj.transform.childCount > 1)
- // {
- // if (resLayer == 1)
- // {
- // GameObject.DestroyImmediate(parentGameObj.transform.GetChild(1).gameObject);
- // }
- // else if (resLayer == 2)
- // {
- // GameObject.DestroyImmediate(parentGameObj.transform.GetChild(0).gameObject);
- // }
- // else if (resLayer == 3)
- // {
- // }
- // }
- SceneController.SetBoxCollider2DToGameObject(parentGameObj.transform.GetChild(0).gameObject);
- }
- //向Transform添加SpriteRenderer并设置资源
- public static void SetSpriteRendererToTransform(Transform tf, string resPath)
- {
- tf.position = Vector3.zero;
- SpriteRenderer spr = tf.GetComponent<SpriteRenderer>();
- if (spr == null)
- {
- tf.gameObject.AddComponent<SpriteRenderer>();
- spr = tf.GetComponent<SpriteRenderer>();
- }
- DressUpUtil.ChangeAssetReleaser(tf.gameObject, resPath);
- Sprite sp = GFGAsset.Load<Sprite>(resPath);
- spr.sprite = sp;
- spr.size = spr.sprite.bounds.size;//将节点设置为原图大小
- ItemTypeCfg typeCfg = ItemTypeCfgArray.Instance.GetCfg(ConstDressUpItemType.BEI_JING);
- }
- //向GameObject添加BoxCollider2D
- public static void SetBoxCollider2DToGameObject(GameObject gameObject)
- {
- BoxCollider2D polygonCollider2D = gameObject.GetComponent<BoxCollider2D>();
- if (polygonCollider2D != null)
- {
- GameObject.Destroy(polygonCollider2D);
- }
- polygonCollider2D = gameObject.AddComponent<BoxCollider2D>();
- polygonCollider2D.isTrigger = true;
- }
- //传入父物体,返回体碰撞盒大小
- public static Vector2 GetGameObjectSize(GameObject parentObj)
- {
- Vector2 size = Vector2.one;
- for (int i = 0; i < parentObj.transform.childCount; i++)
- {
- GameObject childGameObj = parentObj.transform.GetChild(i).gameObject;
- BoxCollider2D boxCollider2D = childGameObj.GetComponent<BoxCollider2D>();
- if (boxCollider2D != null)
- {
- size = GetGameObjectBoundsSize(parentObj);
- if (parentObj.name == "Role") boxCollider2D.size = size;
- boxCollider2D.offset = -childGameObj.transform.localPosition;
- //* childGameObj.transform.localScale
- size = size * parentObj.transform.localScale * 100;
- return size;
- }
- }
- return Vector2.zero;
- }
- private static Vector2 GetGameObjectBoundsSize(GameObject parentObj)
- {
- float right = int.MinValue;
- float left = int.MaxValue;
- float top = int.MinValue;
- float bottom = int.MaxValue;
- for (int i = 0; i < parentObj.transform.childCount; i++)
- {
- Transform transform = parentObj.transform.GetChild(i);
- SpriteRenderer sr = transform.GetComponent<SpriteRenderer>();
- if (sr != null)
- {
- Vector2 pos = transform.localPosition;
- Vector2 size = sr.bounds.size / parentObj.transform.localScale.x;
- Debug.Log("size:" + size + " pos:" + pos);
- right = Math.Max(size.x / 2 + pos.x, right);
- left = Math.Min(pos.x - size.x / 2, left);
- top = Math.Max(size.y / 2 + pos.y, top);
- bottom = Math.Min(pos.y - size.y / 2, bottom);
- }
- }
- Vector2 bounds = new Vector2(right - left, top - bottom);
- Debug.Log("size:" + bounds);
- return bounds;
- }
- //设置物体中心点
- public static void SetGameObjectCenter(GameObject parentObj)
- {
- Transform parent = parentObj.transform; // 2.选中技算
- Vector3 postion = parent.position;
- Quaternion rotation = parent.rotation;
- Vector3 scale = parent.localScale;
- parent.position = Vector3.zero;
- parent.rotation = Quaternion.Euler(Vector3.zero);
- parent.localScale = Vector3.one;
- Vector3 center = Vector3.zero;
- Renderer[] renders = parent.GetComponentsInChildren<Renderer>();
- int index = 0;
- foreach (Transform t in parent)
- {
- string[] strs = t.name.Split('_');
- if (strs.Length > 1 && strs[1] == "eff") continue;//不计算特效大小
- Renderer render = t.GetComponent<Renderer>();
- if (render)
- {
- index++;
- center += render.bounds.center;
- }
- }
- center /= index;
- Bounds bounds = new Bounds(center, Vector3.zero);
- foreach (Transform t in parent)
- {
- string[] strs = t.name.Split('_');
- if (strs.Length > 1 && strs[1] == "eff") continue;
- Renderer render = t.GetComponent<Renderer>();
- if (render) bounds.Encapsulate(render.bounds);
- }
- parent.position = postion;
- parent.rotation = rotation;
- parent.localScale = scale;
- foreach (Transform t in parent)
- {
- string[] strs = t.parent.name.Split('_');
- if (strs.Length > 1 && strs[1] == "eff") continue;
- t.position = t.position - bounds.center;
- }
- parent.position = bounds.center + parent.position;
- }
- public static GameObject GetFirstHitObj(RaycastHit2D[] hit2Ds)
- {
- int layer = int.MinValue;
- GameObject gameObject = null;
- for (int i = 0; i < hit2Ds.Length; i++)
- {
- SpriteRenderer spr = hit2Ds[i].collider.gameObject.GetComponent<SpriteRenderer>();
- if (spr && spr.sortingOrder > layer)
- {
- gameObject = hit2Ds[i].collider.gameObject;
- layer = spr.sortingOrder;
- }
- }
- return gameObject;
- }
- public static void AddObjectToView(GameObject _gameObject, GoWrapper _wrapper, GGraph holder, string res, out GameObject gameObject, out GoWrapper wrapper, float scale = 100)
- {
- if (_gameObject != null)
- {
- SpriteRenderer spr = _gameObject.GetComponent<SpriteRenderer>();
- if (spr != null)
- {
- DressUpUtil.TryClearSpriteObj(_gameObject, spr.name);
- _gameObject = null;
- }
- }
- _gameObject = DressUpUtil.CreateAnimationObj(res);
- _gameObject.transform.localScale = new Vector3(scale, scale, scale);
- if (_wrapper == null)
- {
- _wrapper = new GoWrapper(_gameObject);
- holder.SetNativeObject(_wrapper);
- }
- else
- {
- GameObject.Destroy(_wrapper.wrapTarget);
- _wrapper.wrapTarget = _gameObject;//替换资源
- }
- wrapper = _wrapper;
- gameObject = _gameObject;
- }
- public static void DestroyObjectFromView(GameObject _gameObject, GoWrapper goWrapper)
- {
- if (_gameObject != null)
- {
- SpriteRenderer spr = _gameObject.GetComponent<SpriteRenderer>();
- if (spr != null)
- {
- DressUpUtil.TryClearSpriteObj(_gameObject, spr.name);
- _gameObject = null;
- }
- }
- if (goWrapper != null)
- {
- goWrapper.Dispose();
- }
- }
- }
- }
|