GetMaterial
How do I use Get Material
Below are practical examples compiled from projects for learning and reference purposes
Featured Snippets
File name: MaterialPerOwner.cs
Copy
18 private void Update()
19 {
20 if( this.photonView.ownerId != assignedColorForUserId )
21 {
22 m_Renderer.material = PlayerVariables.GetMaterial( m_Renderer.material, this.photonView.ownerId );
23 this.assignedColorForUserId = this.photonView.ownerId;
24 //Debug.Log("Switched Material to: " + this.assignedColorForUserId + " " + this.renderer.material.GetInstanceID());
25 }
26 }
File name: PlayerVariables.cs
Copy
27 public static Material GetMaterial(Material original, int playerId)
28 {
29 Material result = playerMaterials[playerId%playerMaterials.Length];
30
31 if (result == null)
32 {
33 result = new Material(original);
34 result.color = GetColor(playerId);
35 playerMaterials[playerId%playerMaterials.Length] = result;
36 }
37
38 return result;
39 }
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: ImportTiled2Unity.Texture.cs
Copy
14 public void TextureImported(string texturePath)
15 {
16 // This is fixup method due to materials and textures, under some conditions, being imported out of order
17 Texture2D texture2d = AssetDatabase.LoadAssetAtPath(texturePath, typeof(Texture2D)) as Texture2D;
18 Material material = AssetDatabase.LoadAssetAtPath(ImportUtils.GetMaterialPath(texturePath), typeof(Material)) as Material;
19 material.SetTexture("_MainTex", texture2d);
20 }
File name: ImportTiled2Unity.Xml.cs
Copy
42 private void ImportTexturesFromXml(XDocument xml)
43 {
44 var texData = xml.Root.Elements("ImportTexture");
45 foreach (var tex in texData)
46 {
47 string name = tex.Attribute("filename").Value;
48 string data = tex.Value;
49
50 // The data is gzip compressed base64 string. We need the raw bytes.
51 //byte[] bytes = ImportUtils.GzipBase64ToBytes(data);
52 byte[] bytes = ImportUtils.Base64ToBytes(data);
53
54 // Save and import the texture asset
55 {
56 string pathToSave = ImportUtils.GetTexturePath(name);
57 ImportUtils.ReadyToWrite(pathToSave);
58 File.WriteAllBytes(pathToSave, bytes);
59 AssetDatabase.ImportAsset(pathToSave, ImportAssetOptions.ForceSynchronousImport);
60 }
61
62 // Create a material if needed in prepartion for the texture being successfully imported
63 {
64 string materialPath = ImportUtils.GetMaterialPath(name);
65 Material material = AssetDatabase.LoadAssetAtPath(materialPath, typeof(Material)) as Material;
66 if (material == null)
67 {
68 // We need to create the material afterall
69 // Use our custom shader
70 material = new Material(Shader.Find("Tiled/TextureTintSnap"));
71 ImportUtils.ReadyToWrite(materialPath);
72 AssetDatabase.CreateAsset(material, materialPath);
73 }
74 }
75 }
76 }
File name: ImportTiled2Unity.Xml.cs
Copy
78 private void CreateMaterialsFromInternalTextures(XDocument xml)
79 {
80 var texData = xml.Root.Elements("InternalTexture");
81 foreach (var tex in texData)
82 {
83 string texAssetPath = tex.Attribute("assetPath").Value;
84 string materialPath = ImportUtils.GetMaterialPath(texAssetPath);
85
86 Material material = AssetDatabase.LoadAssetAtPath(materialPath, typeof(Material)) as Material;
87 if (material == null)
88 {
89 // Create our material
90 material = new Material(Shader.Find("Tiled/TextureTintSnap"));
91
92 // Assign to it the texture that is already internal to our Unity project
93 Texture2D texture2d = AssetDatabase.LoadAssetAtPath(texAssetPath, typeof(Texture2D)) as Texture2D;
94 material.SetTexture("_MainTex", texture2d);
95
96 // Write the material to our asset database
97 ImportUtils.ReadyToWrite(materialPath);
98 AssetDatabase.CreateAsset(material, materialPath);
99 }
100 }
101 }
File name: ImportUtils.cs
Copy
17 public static string GetMaterialPath(string texName)
18 {
19 return String.Format("Assets/Tiled2Unity/Materials/{0}.mat", Path.GetFileNameWithoutExtension(texName));
20 }
GetMaterial 142 lượt xem
Gõ tìm kiếm nhanh...