// ============================== PRIVATE FUNC ============================== private void SetingUpPosOfElement(List <ObjLocation> lElementInfo) { Vector2 screenSize = new Vector2(Screen.width, Screen.height); for (int i = 0; i < lElementInfo.Count; i++) { ObjLocation elementRect = lElementInfo[i]; for (int j = 0; j < transform.childCount; j++) { GameObject elementObj = transform.GetChild(j).gameObject; if (elementObj.name == elementRect.m_ObjName) { // set scale for element Vector2 hudSize = new Vector2(elementRect.m_Rect.width * screenSize.x, elementRect.m_Rect.height * screenSize.y); RectTransform rt = elementObj.GetComponent <RectTransform>(); rt.sizeDelta = hudSize; // set position for element rt.position = new Vector3((elementRect.m_Rect.x * screenSize.x) + rt.sizeDelta.x / 2.0f, (elementRect.m_Rect.y * screenSize.y) - rt.sizeDelta.y / 2.0f, 0.0f); break; } } } }
protected void SetLocationForElement(GElement gElement) { // detect element following its name int index = m_SceneInfo.m_lElementLoc.FindIndex(x => x.m_ObjName == gElement.gameObject.name); if (index != -1) { Rect camRect = CameraController.GetCamRectInEditor(); GameObject elementObj = gElement.gameObject; SpriteRenderer elementSr = elementObj.GetComponent <SpriteRenderer>(); // set SIZE of Element ObjLocation objLoc = m_SceneInfo.m_lElementLoc[index]; Vector3 elementScale = elementObj.transform.localScale; if (objLoc.m_ScaleSameByY != 0) { float scale = (camRect.height * objLoc.m_ScaleSameByY * 100.0f) / elementSr.sprite.rect.height; elementScale = new Vector3(scale, scale, 1.0f); } else { elementScale = new Vector3(camRect.width * objLoc.m_Rect.width, camRect.height * objLoc.m_Rect.height, 1.0f); } elementObj.transform.localScale = elementScale; Vector2 topleftCamPos = CameraController.s_Instance.GetTopLeftCamPos(); // set POS of Element Rect objRect = objLoc.m_Rect; Vector3 elementPos = elementObj.transform.position; elementPos.x = topleftCamPos.x + ((objLoc.m_Rect.x * camRect.width) + elementSr.bounds.extents.x); elementPos.y = topleftCamPos.y - ((objLoc.m_Rect.y * camRect.height) + elementSr.bounds.extents.y); elementSr.transform.position = elementPos; } }
protected void SetLocationForElement(GameObject a_hudElementObj) { int index = m_HUDInfo.m_lElementLoc.FindIndex(x => x.m_ObjName == a_hudElementObj.name); if (index != -1) { Vector2 screenSize = CameraController.GetScreenSize(); GameObject elementObj = a_hudElementObj; RectTransform elementRt = elementObj.GetComponent <RectTransform>(); // set Scale of Element ObjLocation objLoc = m_HUDInfo.m_lElementLoc[index]; Vector3 elementScale = elementRt.localScale; if (objLoc.m_ScaleSameByY != 0) { elementScale = Vector3.one * ((objLoc.m_ScaleSameByY * screenSize.y) / elementRt.sizeDelta.y); } else { elementScale.x = (objLoc.m_Rect.width * screenSize.x) / elementRt.sizeDelta.x; elementScale.y = (objLoc.m_Rect.height * screenSize.y) / elementRt.sizeDelta.y; } elementScale.z = 1.0f; elementRt.localScale = elementScale; // set POS of Element Rect objRect = objLoc.m_Rect; Vector3 elementPos = elementRt.position; elementPos.x = (objLoc.m_Rect.x + (objLoc.m_Rect.width / 2.0f)) * screenSize.x; elementPos.y = (objLoc.m_Rect.y - (objLoc.m_Rect.height / 2.0f)) * screenSize.y; elementRt.position = elementPos; } }
// ================================== UNITY FUNCS ================================== #region Unity funcs private void Update() { Vector2 screenSize = CameraController.GetScreenSize(); for (int i = 0; i < transform.childCount; i++) { GameObject hudObj = transform.GetChild(i).gameObject; if (!hudObj.active) { continue; } for (int j = 0; j < m_lHUDDesignInfo.Count; j++) { HUDDesignInfo hudInfo = m_lHUDDesignInfo[j]; if (hudObj == hudInfo.m_Pref) { // update scene info for (int k = 0; k < hudObj.transform.childCount; k++) { GameObject sceneElementObj = hudObj.transform.GetChild(k).gameObject; if (!sceneElementObj.active) { continue; } ObjLocation objLocation = hudInfo.m_lElementLoc.Find(x => x.m_ObjName == sceneElementObj.name); // update location rect of element if (objLocation != null) { // scale for all width & height if (objLocation.m_ScaleSameByY != 0) { Image img = sceneElementObj.GetComponent <Image>(); float elementScale = (screenSize.y * objLocation.m_ScaleSameByY) / img.sprite.rect.height; RectTransform rt = sceneElementObj.GetComponent <RectTransform>(); //rt.sizeDelta = new Vector2(img.sprite.rect.width, img.sprite.rect.height) * elementScale; rt.sizeDelta = new Vector2(img.sprite.rect.width, img.sprite.rect.height); rt.localScale = Vector3.one * elementScale; } objLocation.m_Rect = GetRectOfObjOnCanvas(screenSize, sceneElementObj); } // add new rect of element else { objLocation = new ObjLocation(); objLocation.m_ObjName = sceneElementObj.name; objLocation.m_Rect = GetRectOfObjOnCanvas(screenSize, sceneElementObj); hudInfo.m_lElementLoc.Add(objLocation); } } break; } } } }
// ================================== UNITY FUNCS ================================== #region Unity funcs private void Update() { Rect camRect = CameraController.GetCamRectInEditor(); for (int i = 0; i < transform.childCount; i++) { GameObject sceneObj = transform.GetChild(i).gameObject; if (!sceneObj.active) { continue; } for (int j = 0; j < m_lSceneDesignInfo.Count; j++) { SceneDesignInfo sceneInfo = m_lSceneDesignInfo[j]; if (sceneObj == sceneInfo.m_Pref) { if (sceneInfo.m_lElementLoc.Count != sceneObj.transform.childCount) { sceneInfo.m_lElementLoc.Clear(); } // update scene info for (int k = 0; k < sceneObj.transform.childCount; k++) { GameObject sceneElementObj = sceneObj.transform.GetChild(k).gameObject; ObjLocation objLocation = sceneInfo.m_lElementLoc.Find(x => x.m_ObjName == sceneElementObj.name); // update location rect of element if (objLocation != null) { // scale for all width & height if (objLocation.m_ScaleSameByY != 0) { SpriteRenderer srElement = sceneElementObj.GetComponent <SpriteRenderer>(); float elementScale = (camRect.height * objLocation.m_ScaleSameByY * 100.0f) / srElement.sprite.rect.height; sceneElementObj.transform.localScale = new Vector3(elementScale, elementScale, 1.0f); } objLocation.m_Rect = GetRectOfObjOnScene(camRect, sceneElementObj); } // add new rect of element else { objLocation = new ObjLocation(); objLocation.m_ObjName = sceneElementObj.name; objLocation.m_Rect = GetRectOfObjOnScene(camRect, sceneElementObj); sceneInfo.m_lElementLoc.Add(objLocation); } } break; } } } }
public ObjLocation(ObjLocation a_Copy) { m_ObjName = a_Copy.m_ObjName; m_Rect = a_Copy.m_Rect; }