123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345 |
- using UnityEngine;
- using System;
- using FairyGUI;
- 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.UpdateBodyOnly("ui_loginrole", sceneObj, true);
- }
- public static void UpdateMainScene(GameObject sceneObj)
- {
- //背景0
- Transform tf = sceneObj.transform.Find("Bg");
- SpriteRenderer spr = tf.GetComponent<SpriteRenderer>();
- var resPath = ResPathUtil.GetBgImgPath("zjm_1");
- Sprite sp = GFGAsset.Load<Sprite>(resPath);
- DressUpUtil.AddAssetReleaser(tf.gameObject, resPath);
- spr.sprite = sp;
- //背景1
- Transform tf1 = sceneObj.transform.Find("Bg1");
- SpriteRenderer spr1 = tf1.GetComponent<SpriteRenderer>();
- var resPath1 = ResPathUtil.GetBgImgPath("zjm_2");
- Sprite sp1 = GFGAsset.Load<Sprite>(resPath1);
- DressUpUtil.AddAssetReleaser(tf1.gameObject, resPath);
- spr1.sprite = sp1;
- //角色
- CustomSuitData suitSavedData = CustomSuitDataManager.GetCurrentSuitList();
- MyDressUpHelper.dressUpObj.setSceneObj(sceneObj, false,true,null,false);
- MyDressUpHelper.dressUpObj.PutOnDressUpData(suitSavedData.dressUpData);
- }
- 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;
- }
- //拍照角色
- public static void UpdatePhotographBody(GameObject sceneObj, GameObject parentObj)
- {
- PhotographDataManager.Instance.dressUpObj.setSceneObj(sceneObj,false, false, parentObj, false);
- PhotographDataManager.Instance.dressUpObj.PutOnDressUpData(MyDressUpHelper.dressUpObj.DressUpDataClone());
- GameObject gameObject = parentObj.transform.Find("Head").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;
- }
- //移除指定GameObject的BoxCollider2D
- public static void DeleteBoxCollider2DFromGameObject(GameObject gameObject)
- {
- BoxCollider2D polygonCollider2D = gameObject.GetComponent<BoxCollider2D>();
- if (polygonCollider2D != null)
- {
- GameObject.Destroy(polygonCollider2D);
- }
- }
- //传入父物体,返回体碰撞盒大小
- 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)
- {
- GameObject.DestroyImmediate(_gameObject);
- }
- _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)
- {
- GameObject.DestroyImmediate(_gameObject);
- }
- if (goWrapper != null)
- {
- goWrapper.Dispose();
- }
- }
- }
- }
|