MeshRenderer









How do I use Mesh Renderer
Below are practical examples compiled from projects for learning and reference purposes

Featured Snippets


File name: ShowInfoOfPlayer.cs Copy
20     void Start()
21     {
22         if (font == null)
23         {
24             #if UNITY_3_5
25             font = (Font)FindObjectsOfTypeIncludingAssets(typeof(Font))[0];
26             #else
27             font = (Font)Resources.FindObjectsOfTypeAll(typeof(Font))[0];
28             #endif
29             Debug.LogWarning("No font defined. Found font: " + font);
30         }
31
32         if (tm == null)
33         {
34             textGo = new GameObject("3d text");
35             //textGo.transform.localScale = new Vector3(0.2f, 0.2f, 0.2f);
36             textGo.transform.parent = this.gameObject.transform;
37             textGo.transform.localPosition = Vector3.zero;
38
39             MeshRenderer mr = textGo.AddComponent();
40             mr.material = font.material;
41             tm = textGo.AddComponent();
42             tm.font = font;
43             tm.anchor = TextAnchor.MiddleCenter;
44             if (this.CharacterSize > 0)
45             {
46                 tm.characterSize = this.CharacterSize;
47             }
48         }
49     }
File name: BlockScript.cs Copy
94  public void setBlockNumber(int blockNumber) {
95   this.blockNumber = blockNumber;
96   TextMesh textMesh = this.GetComponentInChildren();
97   Material blockTextMaterial = gameObject.transform.Find ("BlockText").gameObject.GetComponent().material;
98   MeshRenderer cube = gameObject.GetComponentInChildren();
99   Color cubeColor = cube.GetComponent().material.color;
100
101   //block doesn't exits
102   if(blockNumber == -2) {
103    cube.GetComponent().enabled = false;
104    textMesh.text = "";
105   }
106   //block is empty
107   else if (blockNumber == -1 ) {
108    cube.GetComponent().enabled = false;
109    cube.GetComponent().material.color = new Color(1, 1, 1, 0.2f);
110    textMesh.text = "";
111   }
112   //block has a value
113   else {
114    cube.GetComponent().enabled = true;
115    cube.material.color = this.getColor (blockNumber);
116    blockTextMaterial.SetColor ("_Color", new Color(1,1,1));
117    if(blockNumber == 0) blockTextMaterial.SetColor ("_Color", new Color(0.8f,0.8f,1));
118    textMesh.text = blockNumber.ToString();
119    transform.position = this.originalPosition;
120   }
121
122  }
File name: PlayerControl.cs Copy
23     void Awake()
24     {
25         rb = GetComponent();
26         pools = GameObject.FindObjectOfType();
27         ui = GameObject.FindObjectOfType();
28         sounds = GameObject.FindObjectOfType();
29         mesh = GetComponent();
30     }
File name: ImportTiled2Unity.Material.cs Copy
19         public Material FixMaterialForMeshRenderer(string objName, Renderer renderer)
20         {
21             string xmlPath = ImportUtils.GetXmlPath(objName);
22
23             XDocument xml = XDocument.Load(xmlPath);
24
25             // The mesh to match
26             string meshName = renderer.name;
27
28             // The mesh name may be decorated by Unity
29             string pattern = @"_MeshPart[\d]$";
30             Regex regex = new Regex(pattern);
31             meshName = regex.Replace(meshName, "");
32
33             var assignMaterials = xml.Root.Elements("AssignMaterial");
34
35             // Find an assignment that matches the mesh renderer
36             XElement match = assignMaterials.FirstOrDefault(el => el.Attribute("mesh").Value == meshName);
37
38             if (match == null)
39             {
40                 // The names of our meshes in the AssignMaterials elements may be wrong
41                 // This happened before when Unity replaced whitespace with underscore in our named meshes
42                 // That case is handled now, but there may be others
43                 StringBuilder builder = new StringBuilder();
44                 builder.AppendFormat("Could not find mesh named '{0}' for material matching\n", renderer.name);
45                 string choices = String.Join("\n ", assignMaterials.Select(m => m.Attribute("mesh").Value).ToArray());
46                 builder.AppendFormat("Choices are:\n {0}", choices);
47
48                 Debug.LogError(builder.ToString());
49                 return null;
50             }
51
52             string materialName = match.Attribute("material").Value;
53             string materialPath = ImportUtils.GetMaterialPath(materialName);
54
55             // Assign the material
56             renderer.sharedMaterial = AssetDatabase.LoadAssetAtPath(materialPath, typeof(Material)) as Material;
57
58             // Set the sorting layer for the mesh
59             string sortingLayer = match.Attribute("sortingLayerName").Value;
60             if (!String.IsNullOrEmpty(sortingLayer) && !SortingLayerExposedEditor.GetSortingLayerNames().Contains(sortingLayer))
61             {
62                 Debug.LogError(string.Format("Sorting Layer \"{0}\" does not exist. Check your Project Settings -> Tags and Layers", sortingLayer));
63                 renderer.sortingLayerName = "Default";
64             }
65             else
66             {
67                 renderer.sortingLayerName = sortingLayer;
68             }
69
70             // Set the sorting order
71             renderer.sortingOrder = ImportUtils.GetAttributeAsInt(match, "sortingOrder");
72
73             // Do we have an alpha color key?
74             string htmlColor = ImportUtils.GetAttributeAsString(match, "alphaColorKey", "");
75             if (!String.IsNullOrEmpty(htmlColor))
76             {
77                 // Take for granted color is in the form '#RRGGBB'
78                 byte r = byte.Parse(htmlColor.Substring(1, 2), System.Globalization.NumberStyles.HexNumber);
79                 byte g = byte.Parse(htmlColor.Substring(3, 2), System.Globalization.NumberStyles.HexNumber);
80                 byte b = byte.Parse(htmlColor.Substring(5, 2), System.Globalization.NumberStyles.HexNumber);
81                 Color color = new Color32(r, g, b, 255);
82                 renderer.sharedMaterial.SetColor("_AlphaColorKey", color);
83             }
84
85             return renderer.sharedMaterial;
86         }
File name: TiledAssetPostProcessor.cs Copy
111         private void OnPostprocessModel(GameObject gameObject)
112         {
113             if (!UseThisImporter())
114                 return;
115
116             // Each mesh renderer has the ability to set the a sort layer but it takes some work with Unity to expose it.
117             foreach (MeshRenderer mr in gameObject.GetComponentsInChildren())
118             {
119                 mr.gameObject.AddComponent();
120
121                 // Also, no shadows
122                 mr.receiveShadows = false;
123                 mr.castShadows = false;
124             }
125         }
File name: TiledAssetPostProcessor.cs Copy
127         private Material OnAssignMaterialModel(Material defaultMaterial, Renderer renderer)
128         {
129             if (!UseThisImporter())
130                 return null;
131
132             // This is the only reliable place to assign materials in the import chain.
133             // It kind of sucks because we have to go about making the mesh/material association in a roundabout way.
134
135             // Note: This seems dangerous, but getting to the name of the base gameObject appears to be take some work.
136             // The root gameObject, at this point, seems to have "_root" appeneded to it.
137             // Once the model if finished being imported it drops this postifx
138             // This is something that could change without our knowledge
139             string rootName = renderer.transform.root.gameObject.name;
140             int rootIndex = rootName.LastIndexOf("_root");
141             if (rootIndex != -1)
142             {
143                 rootName = rootName.Remove(rootIndex);
144             }
145
146             ImportTiled2Unity importer = new ImportTiled2Unity();
147             return importer.FixMaterialForMeshRenderer(rootName, renderer);
148         }

MeshRenderer 166 lượt xem

Gõ tìm kiếm nhanh...