本文整理汇总了C#中UnityEngine.Material.GetInstanceID方法的典型用法代码示例。如果您正苦于以下问题:C# Material.GetInstanceID方法的具体用法?C# Material.GetInstanceID怎么用?C# Material.GetInstanceID使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类UnityEngine.Material
的用法示例。
在下文中一共展示了Material.GetInstanceID方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AddMaterialReference
/// <summary>
/// Function to add a new material reference and returning its index in the material reference array.
/// </summary>
/// <param name="material"></param>
/// <param name="fontAsset"></param>
/// <param name="materialReferences"></param>
/// <param name="materialReferenceIndexLookup"></param>
/// <returns></returns>
public static int AddMaterialReference(Material material, TMP_FontAsset fontAsset, MaterialReference[] materialReferences, Dictionary<int, int> materialReferenceIndexLookup)
{
int materialID = material.GetInstanceID();
int index = 0;
if (materialReferenceIndexLookup.TryGetValue(materialID, out index))
{
return index;
}
else
{
index = materialReferenceIndexLookup.Count;
// Add new reference index
materialReferenceIndexLookup[materialID] = index;
materialReferences[index].index = index;
materialReferences[index].fontAsset = fontAsset;
materialReferences[index].spriteAsset = null;
materialReferences[index].material = material;
materialReferences[index].isDefaultMaterial = materialID == fontAsset.material.GetInstanceID() ? true : false;
//materialReferences[index].padding = 0;
materialReferences[index].referenceCount = 0;
return index;
}
}
示例2: FromMaterial
public static SMaterial FromMaterial(Material mat)
{
if (mat == null)
return null;
Shader shader = mat.shader;
if (shader == null)
return null;
SMaterial result = new SMaterial();
result.instanceID = mat.GetInstanceID();
result.materialName = mat.name;
result.shaderName = shader.name;
ShaderProperties.Info info = ShaderPropertyHelper.GetShaderInfo(shader.name);
if (info != null){
ComposedByteStream rawData = new ComposedByteStream();
int iMax = info.propertyNames.Length;
for (int i = 0; i < iMax; i++)
{
string propName = info.propertyNames[i];
switch (info.propertyTypes[i])
{
case ShaderProperties.PropType.Color:
Color color = mat.GetColor(propName);
rawData.AddStream(new float[] { color.r, color.g, color.b, color.a });
break;
case ShaderProperties.PropType.Range:
case ShaderProperties.PropType.Float:
rawData.AddStream(new float[] { mat.GetFloat(propName) });
break;
case ShaderProperties.PropType.TexEnv:
Texture texture = mat.GetTexture(propName);
Vector2 offset = mat.GetTextureOffset(propName);
Vector2 scale = mat.GetTextureScale(propName);
rawData.AddStream(new int[] { texture != null ? texture.GetInstanceID() : -1 });
rawData.AddStream(texture != null ? texture.name : "" );
rawData.AddStream(new float[] { offset.x, offset.y });
rawData.AddStream(new float[] { scale.x, scale.y });
break;
case ShaderProperties.PropType.Vector:
Vector4 vector = mat.GetVector(propName);
rawData.AddStream(new float[] { vector.x, vector.y, vector.z, vector.w });
break;
}
}
result.rawData = rawData.Compose();
return result;
} else {
if (vBug.settings.general.debugMode)
Debug.LogError("No shader-info found @" + shader.name);
return null;
}
}
示例3: MaterialReference
/// <summary>
/// Constructor for new Material Reference.
/// </summary>
/// <param name="index"></param>
/// <param name="fontAsset"></param>
/// <param name="spriteAsset"></param>
/// <param name="material"></param>
/// <param name="padding"></param>
public MaterialReference(int index, TMP_FontAsset fontAsset, TMP_SpriteAsset spriteAsset, Material material, float padding)
{
this.index = index;
this.fontAsset = fontAsset;
this.spriteAsset = spriteAsset;
this.material = material;
this.isDefaultMaterial = material.GetInstanceID() == fontAsset.material.GetInstanceID() ? true : false;
this.padding = padding;
this.referenceCount = 0;
}
示例4: AddFallbackMaterialReference
/// <summary>
///
/// </summary>
/// <param name="targetMaterial"></param>
public static void AddFallbackMaterialReference(Material targetMaterial)
{
if (targetMaterial == null) return;
int sourceID = targetMaterial.GetInstanceID();
long key;
// Lookup key to retrieve
if (m_fallbackMaterialLookup.TryGetValue(sourceID, out key))
{
FallbackMaterial fallback;
if (m_fallbackMaterials.TryGetValue(key, out fallback))
{
fallback.count += 1;
}
}
}
示例5: SetFontMaterial
// Function called internally when a new material is assigned via the fontMaterial property.
protected override void SetFontMaterial(Material mat)
{
// Get Shader PropertyIDs if they haven't been cached already.
ShaderUtilities.GetShaderPropertyIDs();
// Check in case Object is disabled. If so, we don't have a valid reference to the Renderer.
// This can occur when the Duplicate Material Context menu is used on an inactive object.
if (m_uiRenderer == null)
m_uiRenderer = GetComponent<CanvasRenderer>();
// Destroy previous instance material.
//if (m_fontMaterial != null && m_fontMaterial.GetInstanceID() != mat.GetInstanceID()) DestroyImmediate(m_fontMaterial);
// Release masking material
if (m_maskingMaterial != null)
{
MaterialManager.ReleaseStencilMaterial(m_maskingMaterial);
m_maskingMaterial = null;
}
// Get Masking ID
m_stencilID = MaterialManager.GetStencilID(gameObject);
// Create Instance Material only if the new material is not the same instance previously used.
if (m_fontMaterial == null || m_fontMaterial.GetInstanceID() != mat.GetInstanceID())
m_fontMaterial = CreateMaterialInstance(mat);
if (m_stencilID > 0)
m_fontMaterial = MaterialManager.SetStencil(m_fontMaterial, m_stencilID);
m_sharedMaterial = m_fontMaterial;
SetShaderDepth(); // Set ZTestMode based on Canvas RenderMode.
m_uiRenderer.SetMaterial(m_sharedMaterial, m_sharedMaterial.mainTexture);
m_padding = GetPaddingForMaterial();
//m_alignmentPadding = ShaderUtilities.GetFontExtent(m_sharedMaterial);
}
示例6: ON_MATERIAL_PROPERTY_CHANGED
// Event received when custom material editor properties are changed.
void ON_MATERIAL_PROPERTY_CHANGED(bool isChanged, Material mat)
{
//Debug.Log("*** ON_MATERIAL_PROPERTY_CHANGED ***");
// Filter events and return if the affected material is not this object's material.
if (mat.GetInstanceID() != m_sharedMaterial.GetInstanceID()) return;
if (m_TextComponent == null) m_TextComponent = GetComponentInParent<TextMeshPro>();
m_padding = GetPaddingForMaterial();
m_TextComponent.havePropertiesChanged = true;
m_TextComponent.SetVerticesDirty();
}
示例7: ReleaseStencilMaterial
/// <summary>
/// Function to release the stencil material.
/// </summary>
/// <param name="stencilMaterial"></param>
public static void ReleaseStencilMaterial(Material stencilMaterial)
{
int stencilMaterialID = stencilMaterial.GetInstanceID();
for (int i = 0; i < m_materialList.Count; i++)
{
if (m_materialList[i].stencilMaterial.GetInstanceID() == stencilMaterialID)
{
if (m_materialList[i].count > 1)
m_materialList[i].count -= 1;
else
{
Object.DestroyImmediate(m_materialList[i].stencilMaterial);
m_materialList.RemoveAt(i);
stencilMaterial = null;
}
break;
}
}
#if DEBUG_ON
ListMaterials();
#endif
}
示例8: SerializedTextures
/// <summary>
/// Serializes textures to FBX format.
/// </summary>
/// <param name="gameObj">Parent GameObject being exported.</param>
/// <param name="newPath">The path to export to.</param>
/// <param name="materials">Materials that holds all the textures.</param>
/// <param name="matObjects">The string with the newly serialized texture file.</param>
/// <param name="connections">The string to connect this to the material.</param>
private static void SerializedTextures(GameObject gameObj, string newPath, Material material, string materialName, bool copyTextures, out string objects, out string connections)
{
// TODO: FBX import currently only supports Diffuse Color and Normal Map
// Because it is undocumented, there is no way to easily find out what other textures
// can be attached to an FBX file so it is imported into the PBR shaders at the same time.
// Also NOTE, Unity 5.1.2 will import FBX files with legacy shaders. This is fix done
// in at least 5.3.4.
StringBuilder objectsSb = new StringBuilder();
StringBuilder connectionsSb = new StringBuilder();
int materialId = Mathf.Abs(material.GetInstanceID());
Texture mainTexture = material.GetTexture("_MainTex");
string newObjects = null;
string newConnections = null;
// Serializeds the Main Texture, one of two textures that can be stored in FBX's sysytem
if(mainTexture != null)
{
SerializeOneTexture(gameObj, newPath, material, materialName, materialId, copyTextures, "_MainTex", "DiffuseColor", out newObjects, out newConnections);
objectsSb.AppendLine(newObjects);
connectionsSb.AppendLine(newConnections);
}
if(SerializeOneTexture(gameObj, newPath, material, materialName, materialId, copyTextures, "_BumpMap", "NormalMap", out newObjects, out newConnections))
{
objectsSb.AppendLine(newObjects);
connectionsSb.AppendLine(newConnections);
}
connections = connectionsSb.ToString();
objects = objectsSb.ToString();
}
示例9: ON_MATERIAL_PROPERTY_CHANGED
// Event received when custom material editor properties are changed.
void ON_MATERIAL_PROPERTY_CHANGED(bool isChanged, Material mat)
{
//Debug.Log("*** ON_MATERIAL_PROPERTY_CHANGED ***");
int targetMaterialID = mat.GetInstanceID();
int sharedMaterialID = m_sharedMaterial.GetInstanceID();
int maskingMaterialID = m_MaskMaterial == null ? 0 : m_MaskMaterial.GetInstanceID();
// Filter events and return if the affected material is not this object's material.
//if (targetMaterialID != sharedMaterialID && targetMaterialID != maskingMaterialID) return;
if (m_TextComponent == null) m_TextComponent = GetComponentInParent<TextMeshProUGUI>();
// Make sure material properties are synchronized between the assigned material and masking material.
if (m_MaskMaterial != null)
{
UnityEditor.Undo.RecordObject(m_MaskMaterial, "Material Property Changes");
UnityEditor.Undo.RecordObject(m_sharedMaterial, "Material Property Changes");
if (targetMaterialID == sharedMaterialID)
{
//Debug.Log("Copy base material properties to masking material if not null.");
float stencilID = m_MaskMaterial.GetFloat(ShaderUtilities.ID_StencilID);
float stencilComp = m_MaskMaterial.GetFloat(ShaderUtilities.ID_StencilComp);
m_MaskMaterial.CopyPropertiesFromMaterial(mat);
m_MaskMaterial.shaderKeywords = mat.shaderKeywords;
m_MaskMaterial.SetFloat(ShaderUtilities.ID_StencilID, stencilID);
m_MaskMaterial.SetFloat(ShaderUtilities.ID_StencilComp, stencilComp);
}
else if (targetMaterialID == maskingMaterialID)
{
// Update the padding
GetPaddingForMaterial(mat);
m_sharedMaterial.CopyPropertiesFromMaterial(mat);
m_sharedMaterial.shaderKeywords = mat.shaderKeywords;
m_sharedMaterial.SetFloat(ShaderUtilities.ID_StencilID, 0);
m_sharedMaterial.SetFloat(ShaderUtilities.ID_StencilComp, 8);
}
}
m_padding = GetPaddingForMaterial();
SetVerticesDirty();
m_ShouldRecalculateStencil = true;
RecalculateClipping();
RecalculateMasking();
}
示例10: GetFallbackMaterial
/// <summary>
/// This function returns a material instance using the material properties of a previous material but using the font atlas texture of the new font asset.
/// </summary>
/// <param name="sourceMaterial">The material containing the source material properties to be copied to the new material.</param>
/// <param name="sourceAtlasTexture">The font atlas texture that should be assigned to the new material.</param>
/// <returns></returns>
public static Material GetFallbackMaterial(Material sourceMaterial, Texture sourceAtlasTexture)
{
int sourceID = sourceMaterial.GetInstanceID();
int texID = sourceAtlasTexture.GetInstanceID();
long key = (long)sourceID << 32 + texID;
FallbackMaterial fallback;
if (m_fallbackMaterials.TryGetValue(key, out fallback))
{
//Debug.Log("Material [" + fallback.fallbackMaterial.name + "] already exists.");
return fallback.fallbackMaterial;
}
// Create new material from the source material
Material fallbackMaterial = new Material(sourceMaterial);
fallbackMaterial.hideFlags = HideFlags.HideAndDontSave;
#if UNITY_EDITOR
fallbackMaterial.name += " + " + sourceAtlasTexture.name;
#endif
fallbackMaterial.SetTexture(ShaderUtilities.ID_MainTex, sourceAtlasTexture);
fallback = new FallbackMaterial();
fallback.baseID = sourceID;
fallback.baseMaterial = sourceMaterial;
fallback.fallbackMaterial = fallbackMaterial;
fallback.count = 0;
m_fallbackMaterials.Add(key, fallback);
m_fallbackMaterialLookup.Add(fallbackMaterial.GetInstanceID(), key);
#if DEBUG_ON
ListFallbackMaterials();
#endif
return fallbackMaterial;
}
示例11: GetMaterial
// Function called internally when a new material is assigned via the fontMaterial property.
protected override Material GetMaterial(Material mat)
{
// Get Shader PropertyIDs if they haven't been cached already.
ShaderUtilities.GetShaderPropertyIDs();
// Check in case Object is disabled. If so, we don't have a valid reference to the Renderer.
// This can occur when the Duplicate Material Context menu is used on an inactive object.
//if (m_canvasRenderer == null)
// m_canvasRenderer = GetComponent<CanvasRenderer>();
// Create Instance Material only if the new material is not the same instance previously used.
if (m_fontMaterial == null || m_fontMaterial.GetInstanceID() != mat.GetInstanceID())
m_fontMaterial = CreateMaterialInstance(mat);
m_sharedMaterial = m_fontMaterial;
m_padding = GetPaddingForMaterial();
SetVerticesDirty();
SetMaterialDirty();
return m_sharedMaterial;
}
示例12: ON_MATERIAL_PROPERTY_CHANGED
// Event received when custom material editor properties are changed.
void ON_MATERIAL_PROPERTY_CHANGED(bool isChanged, Material mat)
{
//Debug.Log("ON_MATERIAL_PROPERTY_CHANGED event received."); // Targeted Material is: " + mat.name + " m_sharedMaterial: " + m_sharedMaterial.name + " with ID:" + m_sharedMaterial.GetInstanceID() + " m_renderer.sharedMaterial: " + m_canvasRenderer.GetMaterial() + " Masking Material:" + m_MaskMaterial.GetInstanceID());
ShaderUtilities.GetShaderPropertyIDs(); // Initialize ShaderUtilities and get shader property IDs.
int materialID = mat.GetInstanceID();
int sharedMaterialID = m_sharedMaterial.GetInstanceID();
int maskingMaterialID = m_MaskMaterial == null ? 0 : m_MaskMaterial.GetInstanceID();
if (m_canvasRenderer.GetMaterial() == null)
{
if (m_fontAsset != null)
{
m_canvasRenderer.SetMaterial(m_fontAsset.material, m_sharedMaterial.mainTexture);
//Debug.LogWarning("No Material was assigned to " + name + ". " + m_fontAsset.material.name + " was assigned.");
}
else
Debug.LogWarning("No Font Asset assigned to " + name + ". Please assign a Font Asset.", this);
}
if (m_canvasRenderer.GetMaterial() != m_sharedMaterial && m_fontAsset == null) // || m_renderer.sharedMaterials.Contains(mat))
{
//Debug.Log("ON_MATERIAL_PROPERTY_CHANGED Called on Target ID: " + GetInstanceID() + ". Previous Material:" + m_sharedMaterial + " New Material:" + m_uiRenderer.GetMaterial()); // on Object ID:" + GetInstanceID() + ". m_sharedMaterial: " + m_sharedMaterial.name + " m_renderer.sharedMaterial: " + m_renderer.sharedMaterial.name);
m_sharedMaterial = m_canvasRenderer.GetMaterial();
}
// Make sure material properties are synchronized between the assigned material and masking material.
if (m_MaskMaterial != null)
{
UnityEditor.Undo.RecordObject(m_MaskMaterial, "Material Property Changes");
UnityEditor.Undo.RecordObject(m_sharedMaterial, "Material Property Changes");
if (materialID == sharedMaterialID)
{
//Debug.Log("Copy base material properties to masking material if not null.");
float stencilID = m_MaskMaterial.GetFloat(ShaderUtilities.ID_StencilID);
float stencilComp = m_MaskMaterial.GetFloat(ShaderUtilities.ID_StencilComp);
//float stencilOp = m_MaskMaterial.GetFloat(ShaderUtilities.ID_StencilOp);
//float stencilRead = m_MaskMaterial.GetFloat(ShaderUtilities.ID_StencilReadMask);
//float stencilWrite = m_MaskMaterial.GetFloat(ShaderUtilities.ID_StencilWriteMask);
m_MaskMaterial.CopyPropertiesFromMaterial(mat);
m_MaskMaterial.shaderKeywords = mat.shaderKeywords;
m_MaskMaterial.SetFloat(ShaderUtilities.ID_StencilID, stencilID);
m_MaskMaterial.SetFloat(ShaderUtilities.ID_StencilComp, stencilComp);
//m_MaskMaterial.SetFloat(ShaderUtilities.ID_StencilOp, stencilOp);
//m_MaskMaterial.SetFloat(ShaderUtilities.ID_StencilReadMask, stencilID);
//m_MaskMaterial.SetFloat(ShaderUtilities.ID_StencilWriteMask, 0);
}
else if (materialID == maskingMaterialID)
{
// Update the padding
GetPaddingForMaterial(mat);
m_sharedMaterial.CopyPropertiesFromMaterial(mat);
m_sharedMaterial.shaderKeywords = mat.shaderKeywords;
m_sharedMaterial.SetFloat(ShaderUtilities.ID_StencilID, 0);
m_sharedMaterial.SetFloat(ShaderUtilities.ID_StencilComp, 8);
//m_sharedMaterial.SetFloat(ShaderUtilities.ID_StencilOp, 0);
//m_sharedMaterial.SetFloat(ShaderUtilities.ID_StencilReadMask, 255);
//m_sharedMaterial.SetFloat(ShaderUtilities.ID_StencilWriteMask, 255);
}
}
m_padding = GetPaddingForMaterial();
m_havePropertiesChanged = true;
SetVerticesDirty();
//SetMaterialDirty();
}
示例13: ON_DRAG_AND_DROP_MATERIAL
// Event to Track Material Changed resulting from Drag-n-drop.
void ON_DRAG_AND_DROP_MATERIAL(Material currentMaterial, Material newMaterial)
{
//Debug.Log("Drag-n-Drop Event - Receiving Object ID " + GetInstanceID() + ". New Material is " + newMaterial.name + " with ID " + newMaterial.GetInstanceID() + ". Base Material is " + m_baseMaterial.name + " with ID " + m_baseMaterial.GetInstanceID());
// Check if event applies to this current object
if (currentMaterial.GetInstanceID() == m_baseMaterial.GetInstanceID())
{
Debug.Log("Assigning new Base Material " + newMaterial.name + " to replace " + currentMaterial.name);
UnityEditor.Undo.RecordObject(this, "Material Assignment");
fontSharedMaterial = newMaterial;
m_baseMaterial = newMaterial;
}
}
示例14: SetSharedFontMaterial
// Function called internally when a new shared material is assigned via the fontSharedMaterial property.
void SetSharedFontMaterial(Material mat)
{
ShaderUtilities.GetShaderPropertyIDs();
// Check in case Object is disabled. If so, we don't have a valid reference to the Renderer.
// This can occur when the Duplicate Material Context menu is used on an inactive object.
if (m_uiRenderer == null)
m_uiRenderer = GetComponent<CanvasRenderer>();
if (mat == null) { mat = m_baseMaterial; m_isNewBaseMaterial = true; }
// Check if Material has Stencil Support
if (mat.HasProperty(ShaderUtilities.ID_StencilID))
{
// Check if Material is a Base Material
if (mat.HasProperty(ShaderUtilities.ID_StencilID) && mat.GetFloat(ShaderUtilities.ID_StencilID) == 0)
{
if (m_baseMaterial == null) m_baseMaterial = m_sharedMaterial; // Addition check in the transition to the use of a Base Material.
// if new Base Material
if (mat.GetInstanceID() != m_baseMaterial.GetInstanceID())
{
m_baseMaterial = mat;
m_isNewBaseMaterial = true;
}
}
else
{
// If new Masking Material
if (mat != m_maskingMaterial)
{
if (m_maskingMaterial != null)
MaterialManager.ReleaseMaskingMaterial(m_maskingMaterial);
//Debug.Log("A New Masking Material [" + mat + "]. Previous Masking Material [" + m_maskingMaterial);
m_maskingMaterial = mat;
MaterialManager.AddMaskingMaterial(m_baseMaterial, m_maskingMaterial, 1);
}
else
{
// Remove masking material from list
//MaterialManager.RemoveMaskingMaterial(m_maskingMaterial);
// Add Masking Material back
//MaterialManager.AddMaskingMaterial(m_baseMaterial, m_maskingMaterial, 1);
//Debug.Log("Masking Material [" + mat.name + "] with ID " + mat.GetInstanceID() + " is assigned.");
}
}
if (m_mask && m_mask.MaskEnabled())
{
if (m_isNewBaseMaterial)
{
if (m_maskingMaterial != null)
MaterialManager.ReleaseMaskingMaterial(m_maskingMaterial);
m_maskingMaterial = MaterialManager.GetMaskingMaterial(m_baseMaterial, 1);
mat = m_maskingMaterial;
}
}
}
m_isNewBaseMaterial = false;
m_uiRenderer.SetMaterial(mat, null);
m_sharedMaterial = mat;
m_padding = ShaderUtilities.GetPadding(m_sharedMaterial, m_enableExtraPadding, m_isUsingBold);
m_alignmentPadding = ShaderUtilities.GetFontExtent(m_sharedMaterial);
//Debug.Log("New Material [" + mat.name + "] with ID " + mat.GetInstanceID() + " has been assigned. Base Material is [" + m_baseMaterial.name + "] with ID " + m_baseMaterial.GetInstanceID());
//MaterialManager.ListMaterials();
}
示例15: SaveMat
public string SaveMat(Material mat)
{
int id = mat.GetInstanceID();
string name = null;
if (savecache.TryGetValue(id, out name))
{
return name;
}
MyJson.JsonNode_Object _json = new MyJson.JsonNode_Object();
//parser.matParser.WriteToJson(this, mat, _json);
#if UNITY_EDITOR
if (parser.GetMatParser(mat.shader.name) == null)
{
var json = nodeParser.GetMatConfig(mat);
parser.InitMatParser(mat.shader.name, json);
#region 自动保存shaderparser配置,这一段可以关闭
string path = Application.dataPath + "/resources/shaderparser";
if (System.IO.Directory.Exists(path) == false)
System.IO.Directory.CreateDirectory(path);
string shadername = mat.shader.name;
shadername = shadername.Replace("/", "%2f");
string file = path + "/" + shadername + ".shaderparser.txt";
if (System.IO.File.Exists(file))
System.IO.File.Delete(file);
System.IO.File.WriteAllText(file, json.ToString());
#endregion
}
#endif
parser.GetMatParser(mat.shader.name).WriteToJson(this, mat, _json);
System.Text.StringBuilder sb = new System.Text.StringBuilder();
_json.ConvertToStringWithFormat(sb, 4);
byte[] bs = System.Text.Encoding.UTF8.GetBytes(sb.ToString());
string sha1 = ResLibTool.ComputeHashString(bs);
name = sha1 + ".jsonmat.txt";
bufs[name] = bs;
savecache[id] = name;
return name;
}