// Update is called once per frame void Update() { if (monitor.Evaluate()) { Debug.Log("A change occurred"); } }
void LateUpdate() { if (monitor.Evaluate()) { if (IsChildOfCanvas) { if (meshRenderer != null) { GameObject.DestroyImmediate(meshRenderer); meshRenderer = null; } var mesh = meshFilter.sharedMesh; if (mesh != null && materials.Length > 0) { var positions = mesh.vertices; var colors = mesh.colors32; var coords = mesh.uv; var coords2 = mesh.uv2; var normals = mesh.normals; var tangents = mesh.tangents; var defaultColor = new Color32(255, 255, 255, 255); var defaultNormal = Vector3.forward; var defaultCoord = Vector2.zero; var defaultTangent = Vector4.zero; for (int i = 0; i < mesh.subMeshCount; i++) { var material = materials[i]; CanvasRenderer canvasRenderer; if (canvasRenderersBySubmeshIndex.ContainsKey(i)) { canvasRenderer = canvasRenderersBySubmeshIndex[i]; } else { canvasRenderersBySubmeshIndex[i] = canvasRenderer = canvasRendererPool.GetObject(); } var triangleIndices = mesh.GetTriangles(i); var triangleCount = triangleIndices.Length / 3; var sourceIndices = new int[triangleCount * 4]; for (int j = 0; j < triangleCount; j++) { sourceIndices[j * 4] = triangleIndices[j * 3]; sourceIndices[j * 4 + 1] = triangleIndices[j * 3]; sourceIndices[j * 4 + 2] = triangleIndices[j * 3 + 1]; sourceIndices[j * 4 + 3] = triangleIndices[j * 3 + 2]; } var uiVertices = new UIVertex[sourceIndices.Length]; for (int j = 0; j < sourceIndices.Length; j++) { var sourceIndex = sourceIndices[j]; uiVertices[j] = new UIVertex { position = positions[sourceIndex], color = sourceIndex < colors.Length ? colors[sourceIndex] : defaultColor, normal = sourceIndex < normals.Length ? normals[sourceIndex] : defaultNormal, tangent = sourceIndex < tangents.Length ? tangents[sourceIndex] : defaultTangent, uv0 = sourceIndex < coords.Length ? coords[sourceIndex] : defaultCoord, uv1 = sourceIndex < coords2.Length ? coords2[sourceIndex] : defaultCoord }; } canvasRenderer.SetMaterial(material, null); canvasRenderer.SetVertices(uiVertices, uiVertices.Length); canvasRenderer.SetColor(Color.white); canvasRenderer.SetAlpha(1f); canvasRenderer.gameObject.SetActive(true); } } var subMeshCount = mesh != null ? mesh.subMeshCount : 0; if (canvasRenderersBySubmeshIndex.Count > subMeshCount) { var oldKeys = (from key in canvasRenderersBySubmeshIndex.Keys where key >= subMeshCount select key).ToArray(); foreach (var key in oldKeys) { var canvasRenderer = canvasRenderersBySubmeshIndex[key]; canvasRenderersBySubmeshIndex.Remove(key); canvasRenderer.Clear(); canvasRendererPool.PutObject(canvasRenderer); } } } else { foreach (var canvasRenderer in canvasRenderersBySubmeshIndex.Values) { DestroyCanvasRenderer(canvasRenderer); } canvasRenderersBySubmeshIndex.Clear(); if (meshRenderer == null) { meshRenderer = gameObject.GetComponent <MeshRenderer>(); } if (meshRenderer == null) { meshRenderer = gameObject.AddComponent <MeshRenderer>(); } meshRenderer.sharedMaterials = materials; } } }