public static List <Vector3> ScreenToPlaneArea(Camera camera, Plane plane, bool containsCameraPos) { List <Vector3> result = new List <Vector3>(); if (containsCameraPos) { Vector3 pos = camera.transform.position; pos.y = 0; result.Add(pos); } Vector3 v1 = Vector3.zero; Vector3 v2 = Vector3.zero; v2.Set(Screen.width, 0, 0); Vector3 v3 = Vector3.zero; v3.Set(0, Screen.height, 0); Vector3 v4 = Vector3.zero; v4.Set(Screen.width, Screen.height, 0); Vector3 p1 = XUtility.ScreenToPlanePoint(camera, plane, v1); Vector3 p2 = XUtility.ScreenToPlanePoint(camera, plane, v2); Vector3 p3 = XUtility.ScreenToPlanePoint(camera, plane, v3); Vector3 p4 = XUtility.ScreenToPlanePoint(camera, plane, v4); result.Add(p1); result.Add(p2); result.Add(p3); result.Add(p4); return(result); }
public static Rect ScreenToGroundAreaAlignedAxis(Camera camera) { Rect rect = new Rect(0, 0, 0, 0); var list = XUtility.ScreenToGroundArea(camera); foreach (var p in list) { if (p.x < rect.xMin) { rect.xMin = p.x; } if (p.x > rect.xMax) { rect.xMax = p.x; } if (p.z < rect.yMin) { rect.yMin = p.z; } if (p.z > rect.yMax) { rect.yMax = p.z; } } return(rect); }
public void Push(string bundleName, GameObject go) { Dictionary <string, LinkedList <GameObject> > category = null; if (!this.mPool.TryGetValue(bundleName, out category)) { category = new Dictionary <string, LinkedList <GameObject> >(); this.mPool.Add(bundleName, category); } string prefabName = XUtility.CutCloneMarkForName(go.name); LinkedList <GameObject> slot = null; if (!category.TryGetValue(prefabName, out slot)) { slot = new LinkedList <GameObject>(); category.Add(prefabName, slot); } if (slot.Count < this.mCountPerGameObject) { slot.AddLast(go); } else { Object.Destroy(go); } }
public static void ForeachGameObjectRecursively(GameObject root, System.Action <GameObject> func) { if (root == null || func == null) { return; } func(root); foreach (Transform t in root.transform) { XUtility.ForeachGameObjectRecursively(t.gameObject, func); } }
public static void ResetRenderQueueRecursively(GameObject root) { XUtility.ForeachComponentRecursively <Renderer>(root, r => { foreach (Material m in r.materials) { if (m == null) { continue; } m.renderQueue = m.shader.renderQueue; } }); }
public static List <int> Subdivision(Mesh mesh) { if (mesh == null || mesh.vertices == null || mesh.triangles == null) { return(null); } List <Vector3> verts = new List <Vector3>(mesh.vertices); List <int> faces = new List <int>(mesh.triangles); List <int> lastLOD = XUtility.Subdivision(verts, faces); mesh.vertices = verts.ToArray(); mesh.triangles = faces.ToArray(); return(lastLOD); }
public GameObject FindUI(Canvas canvas, string uiName) { string uiNameClone = XUtility.AddCloneMarkForName(uiName); GameObject result = null; this.ForeachUI(canvas, ui => { if (ui.name == uiName || ui.name == uiNameClone) { result = ui; return(false); } return(true); }); return(result); }
public static void ForeachComponentRecursively <T>(GameObject root, System.Action <T> func) where T : Component { if (root == null || func == null) { return; } T[] components = root.GetComponents <T>(); foreach (var c in components) { func(c); } foreach (Transform t in root.transform) { XUtility.ForeachComponentRecursively(t.gameObject, func); } }
void Generate() { SkinnedMeshRenderer result = XUtility.FindOrCreateComponent <SkinnedMeshRenderer>(this); List <Transform> skeleton = new List <Transform>(); this.gameObject.GetComponentsInChildren <Transform>(true, skeleton); List <SkinnedMeshRenderer> renderers = new List <SkinnedMeshRenderer>(); foreach (Part p in this.mParts.Values) { if (p.element == null) { continue; } if (p.method == Method.Dress) { SkinnedMeshRenderer[] rs = p.element.GetComponentsInChildren <SkinnedMeshRenderer>(true); renderers.AddRange(rs); if (p.element.transform.parent != this.transform) { p.element.transform.parent = this.transform; } } else { // if p is not changed, dont destroy and attach if (p.changed) { GameObject locator = XUtility.FindGameObjectRecursively(this.gameObject, p.locator); XUtility.DestroyChildren(locator); XUtility.AttachGameObject(locator, p.element); p.element.SetActive(true); //XUtility.SetVisibleRecursively(p.element, true); } } } XUtility.CombineSkinnedMeshRenderer(result, skeleton, renderers); result.updateWhenOffscreen = true; }
public static GameObject FindGameObjectRecursively(GameObject root, int instanceId) { if (root == null) { return(null); } if (root.GetInstanceID() == instanceId) { return(root); } GameObject result = null; foreach (Transform t in root.transform) { result = XUtility.FindGameObjectRecursively(t.gameObject, instanceId); if (result != null) { return(result); } } return(null); }
public static GameObject FindGameObjectRecursively(GameObject root, string name) { if (root == null) { return(null); } if (root.name == name) { return(root); } GameObject result = null; foreach (Transform t in root.transform) { result = XUtility.FindGameObjectRecursively(t.gameObject, name); if (result != null) { return(result); } } return(null); }
public static GameObject FindOrCreateGameObject(GameObject parent, string name) { GameObject result = null; if (parent == null) { result = GameObject.Find(name); if (result == null) { result = new GameObject(name); } } else { result = XUtility.FindGameObjectInChildren(parent, name); if (result == null) { result = new GameObject(name); XUtility.AttachGameObject(parent, result); } } return(result); }
public static void SetLayerRecursively(GameObject root, string layerName) { int layer = LayerMask.NameToLayer(layerName); XUtility.ForeachGameObjectRecursively(root, go => go.layer = layer); }
public static void SetLayerRecursively <T>(GameObject root, string layerName) where T : Component { int layer = LayerMask.NameToLayer(layerName); XUtility.ForeachComponentRecursively <T>(root, c => c.gameObject.layer = layer); }
public static List <Vector3> ScreenToGroundArea(Camera camera) { Plane ground = new Plane(Vector3.up, Vector3.zero); return(XUtility.ScreenToPlaneArea(camera, ground, true)); }
public static void SetColliderEnabledRecursively(GameObject root, bool enabled) { XUtility.ForeachComponentRecursively <Collider>(root, c => c.enabled = enabled); }
// v1 // * // / \ // / \ // m0*-----*m1 // / \ / \ // / \ / \ // *-----*-----* // v0 m2 v2 public static List <int> Subdivision(List <Vector3> verts, List <int> faces) { int iCount = faces.Count; int fCount = faces.Count / 3; for (int f = 0; f < fCount; f++) { int iv0 = faces[f * 3 + 0]; int iv1 = faces[f * 3 + 1]; int iv2 = faces[f * 3 + 2]; Vector3 v0 = verts[iv0]; Vector3 v1 = verts[iv1]; Vector3 v2 = verts[iv2]; Vector3 m0 = (v0 + v1) * 0.5f; Vector3 m1 = (v1 + v2) * 0.5f; Vector3 m2 = (v0 + v2) * 0.5f; int vCount = verts.Count; int vIndex = 0; int im0 = vCount + vIndex; var in0 = XUtility.GetNearestVertices(verts, m0, 0.1f); if (in0.Count == 0) { verts.Add(m0); vIndex++; } else { im0 = in0[0]; } int im1 = vCount + vIndex; var in1 = XUtility.GetNearestVertices(verts, m1, 0.1f); if (in1.Count == 0) { verts.Add(m1); vIndex++; } else { im1 = in1[0]; } int im2 = vCount + vIndex; var in2 = XUtility.GetNearestVertices(verts, m2, 0.1f); if (in2.Count == 0) { verts.Add(m2); vIndex++; } else { im2 = in2[0]; } faces.Add(iv0); faces.Add(im0); faces.Add(im2); faces.Add(iv1); faces.Add(im1); faces.Add(im0); faces.Add(iv2); faces.Add(im2); faces.Add(im1); faces.Add(im0); faces.Add(im1); faces.Add(im2); } List <int> lastLOD = faces.GetRange(0, iCount); faces.RemoveRange(0, iCount); return(lastLOD); }