Объявление
public void SetTexture(string name, Texture value);public void SetTexture(int nameID, Texture value);
public void SetTexture(string name, RenderTexture value, Rendering.RenderTextureSubElement element);
public void SetTexture(int nameID, RenderTexture value, Rendering.RenderTextureSubElement element);
Параметры
nameID | Идентификатор имени свойства. Чтобы получить его, используйте Shader.PropertyToID. |
name | Имя свойства, например. "_MainTex". |
value | Текстура для установки. |
element | Необязательный параметр, указывающий тип данных, которые нужно задать из RenderTexture. |
Описание
Устанавливает именованную текстуру.
Многие шейдеры используют более одной текстуры. Используйте SetTexture, чтобы изменить текстуру (определяемую именем свойства шейдера или уникальным идентификатором имени свойства).
При настройке текстур материалов с помощью стандартного шейдера следует помнить, что вам может потребоваться использовать EnableKeyword, чтобы включить функции шейдера, которые ранее не использовались. . Подробнее см. в разделе Доступ к материалам с помощью скрипта.
Общие имена текстур, используемые встроенными шейдерами Unity: "_MainTex"
— основная диффузная текстура. К этому также можно получить доступ через свойство mainTexture. "_BumpMap"
– это карта нормалей.
В свойствах шейдера также показаны некоторые ключевые слова, необходимые для задания текстуры материала. Чтобы увидеть это, перейдите к своему материалу и щелкните правой кнопкой мыши раскрывающийся список Shader вверху. Затем выберите Выбрать шейдер.
Указав `RenderTextureSubElement`, вы можете указать, какой тип данных следует установить из RenderTexture. Возможные варианты: RenderTextureSubElement.Color, RenderTextureSubElement.Depth и RenderTextureSubElement.Stencil.
Смотрите так же: mainTexture property, GetTexture, Shader.PropertyToID, Properties in Shader Programs, RenderTextureSubElement.
//Attach this script to your GameObject (make sure it has a Renderer component)
//Click on the GameObject. Attach your own Textures in the GameObject’s Inspector.
//This script takes your GameObject’s material and changes its Normal Map, Albedo, and Metallic properties to the Textures you attach in the GameObject’s Inspector. This happens when you enter Play Mode
using UnityEngine;
public class Example : MonoBehaviour {
//Set these Textures in the Inspector
public Texture m_MainTexture, m_Normal, m_Metal;
Renderer m_Renderer;
// Use this for initialization
void Start () {
//Fetch the Renderer from the GameObject
m_Renderer = GetComponent<Renderer> ();
//Make sure to enable the Keywords
m_Renderer.material.EnableKeyword ("_NORMALMAP");
m_Renderer.material.EnableKeyword ("_METALLICGLOSSMAP");
//Set the Texture you assign in the Inspector as the main texture (Or Albedo)
m_Renderer.material.SetTexture("_MainTex", m_MainTexture);
//Set the Normal map using the Texture you assign in the Inspector
m_Renderer.material.SetTexture("_BumpMap", m_Normal);
//Set the Metallic Texture as a Texture you assign in the Inspector
m_Renderer.material.SetTexture ("_MetallicGlossMap", m_Metal);
}
}