private void CreateObject(GameObject layer, LoaderObject node, Vector2 point, Vector2 gridSize, LoaderCallBack cb, LoaderPostCallBack cbPost , LoaderObject loaderObj, sub subObj) { GameObject obj; string layerName; if (layer != null) { layerName = layer.name; } else { layerName = _Name; } //prefab if (node.prefab != null && node.prefab.Length > 0) { obj = Resources.Load <GameObject>(node.prefab); if (node.ui == false) { mPos.SetGameObjectSize(ref obj, gridSize.x, gridSize.y); obj.transform.position = new Vector3(point.x, point.y, 0); } } else { obj = cb(layerName, node.name, node.tag, point, gridSize); } if (obj != null) { obj = UnityEngine.Object.Instantiate(obj); obj.tag = node.tag; obj.name = node.name; //ui if (node.ui == true) { obj.transform.position = mCamera.WorldToScreenPoint(new Vector3(point.x, point.y, 0)); if (layer == null) { obj.transform.SetParent(mCanvas); } else { obj.transform.SetParent(layer.transform); } //resizing if (node.prefab != null && node.prefab.Length > 0) { //span 처리 if (subObj._InnerMargin.x > 0 || subObj._InnerMargin.y > 0) { gridSize.x = (gridSize.x * (loaderObj.span.x + 1)) + (subObj._InnerMargin.x * 2.0f * loaderObj.span.x); gridSize.y = (gridSize.y * (loaderObj.span.y + 1)) + (subObj._InnerMargin.y * 2.0f * loaderObj.span.y); } Vector3 gridScreenSize = mCamera.WorldToScreenPoint(new Vector3(point.x + gridSize.x, point.y + gridSize.y, 0)); gridScreenSize.x -= obj.transform.position.x; gridScreenSize.y -= obj.transform.position.y; obj.GetComponent <RectTransform>().sizeDelta = new Vector2(gridScreenSize.x, gridScreenSize.y); } //onclick Button btn = obj.GetComponent <Button>(); if (btn != null) { btn.onClick.AddListener(() => { mButtonCallBack(obj); }); } } //text if (node.text != null) { Text txt = obj.GetComponent <Text>(); if (txt == null) { txt = obj.GetComponentInChildren <Text>(); } if (txt != null) { txt.text = node.text; } } if (mObjects.ContainsKey(node.name) == false) { mObjects[node.name] = new List <GameObject>(); } mObjects[node.name].Add(obj); if (cbPost != null) { cbPost(obj, layerName); } } }
public void AddComponents(LoaderCallBack cb, LoaderPostCallBack cbPost = null) { List <Vector2> points = mPos.GetGridPoints(_Margin, _GridDim); Vector2 gridSize = mPos.GetGridSize(_Margin, _InnerMargin, _GridDim); mObjects["BG"] = new List <GameObject>(); for (int n = 0; n < _Objects.Count; n++) { int idx = (_Objects[n].position.y * _GridDim.x) + _Objects[n].position.x; if (idx > points.Count) { throw new Exception("Invalid index. check _GridDim & _Objects position"); } sub s = new sub(); CreateObject(null, _Objects[n], GetPosition(points[idx], gridSize, _Objects[n].pivot), gridSize, cb, cbPost, _Objects[n], s); } //_subs for (int i = 0; i < _Subs.Count; i++) { sub s = _Subs[i]; List <Vector2> minMax = mPos.GetGridMinMax(_Margin, _InnerMargin, _GridDim, s._From, s._To); points = mPos.GetGridPoints(s._Margin, s._GridDim, minMax[0], minMax[1]); gridSize = mPos.GetGridSize(minMax[0], minMax[1], s._Margin, s._InnerMargin, s._GridDim); GameObject layer; List <float> bgColor; //bg color if (s._BGColor.Count > 0) { bgColor = s._BGColor; } else { bgColor = new List <float>(); for (int n = 0; n < 4; n++) { bgColor.Add(0.0f); } } layer = CreatePanel(s._Name, minMax, bgColor, s._Margin); mObjects["BG"].Add(layer); for (int n = 0; n < s._Objects.Count; n++) { LoaderObject node = s._Objects[n]; int idx = (node.position.y * s._GridDim.x) + node.position.x; if (idx > points.Count) { throw new Exception("Invalid index. check _GridDim & _Objects position. " + s._Name); } CreateObject(layer, node, GetPosition(points[idx], gridSize, node.pivot), gridSize, cb, cbPost, node, s); } //Draw Line if (mDrawGridSubSet.Contains(s._Name) == true || s._DrawGridLine == true) { mPos.DrawGrid(s._Margin, s._GridDim, minMax[0], minMax[1], mCanvas); } } }