Texture2D
How do I use Texture2 D
Below are practical examples compiled from projects for learning and reference purposes
Featured Snippets
File name: PhotonEditor.cs
Copy
215 static PhotonEditor()
216 {
217 EditorApplication.projectWindowChanged += EditorUpdate;
218 EditorApplication.hierarchyWindowChanged += EditorUpdate;
219 EditorApplication.playmodeStateChanged += PlaymodeStateChanged;
220 EditorApplication.update += OnUpdate;
221
222 WizardIcon = AssetDatabase.LoadAssetAtPath("Assets/Photon Unity Networking/photoncloud-icon.png", typeof(Texture2D)) as Texture2D;
223
224 // to be used in toolbar, the enum needs conversion to string[] being done here, once.
225 Array enumValues = Enum.GetValues(typeof(CloudRegionCode));
226 CloudServerRegionNames = new string[enumValues.Length];
227 for (int i = 0; i < CloudServerRegionNames.Length; i++)
228 {
229 CloudServerRegionNames[i] = enumValues.GetValue(i).ToString();
230 if (CloudServerRegionNames[i].Equals("none"))
231 {
232 CloudServerRegionNames[i] = PhotonEditor.CurrentLang.BestRegionLabel;
233 }
234 }
235
236 // detect optional packages
237 PhotonEditor.CheckPunPlus();
238
239 }
File name: PhotonGUI.cs
Copy
139 {
140 get
141 {
142 if( m_HelpIcon == null )
143 {
144 m_HelpIcon = AssetDatabase.LoadAssetAtPath( "Assets/Photon Unity Networking/Editor/PhotonNetwork/help.png", typeof( Texture2D ) ) as Texture2D;
145 }
146
147 return m_HelpIcon;
148 }
149 }
File name: ReorderableListResources.cs
Copy
125 public static Texture2D texItemSplitter { get; private set; }
File name: ReorderableListResources.cs
Copy
147 public static Texture2D CreatePixelTexture( string name, Color color )
148 {
149 var tex = new Texture2D( 1, 1, TextureFormat.ARGB32, false, true );
150 tex.name = name;
151 tex.hideFlags = HideFlags.HideAndDontSave;
152 tex.filterMode = FilterMode.Point;
153 tex.SetPixel( 0, 0, color );
154 tex.Apply();
155 return tex;
156 }
File name: ReorderableListResources.cs
Copy
168 private static void LoadResourceAssets()
169 {
170 var skin = EditorGUIUtility.isProSkin ? s_DarkSkin : s_LightSkin;
171 s_Cached = new Texture2D[ skin.Length ];
172
173 for( int i = 0; i < s_Cached.Length; ++i )
174 {
175 // Get image data (PNG) from base64 encoded strings.
176 byte[] imageData = Convert.FromBase64String( skin[ i ] );
177
178 // Gather image size from image data.
179 int texWidth, texHeight;
180 GetImageSize( imageData, out texWidth, out texHeight );
181
182 // Generate texture asset.
183 var tex = new Texture2D( texWidth, texHeight, TextureFormat.ARGB32, false, true );
184 tex.hideFlags = HideFlags.HideAndDontSave;
185 tex.name = "(Generated) ReorderableList:" + i;
186 tex.filterMode = FilterMode.Point;
187 tex.LoadImage( imageData );
188
189 s_Cached[ i ] = tex;
190 }
191
192 s_LightSkin = null;
193 s_DarkSkin = null;
194 }
File name: ReorderableListResources.cs
Copy
224 public static void DrawTexture( Rect position, Texture2D texture )
225 {
226 if( Event.current.type != EventType.Repaint )
227 return;
228
229 s_TempStyle.normal.background = texture;
230
231 s_TempStyle.Draw( position, GUIContent.none, false, false, false, false );
232 }
File name: BitmapFont.cs
Copy
26 public BitmapFont(string pathPNG, string pathXML, GameObject gameObject)
27 {
28 this.gameObject = gameObject;
29 sprites = new Dictionary
30 yoffsets_xadvances = new Dictionary
31 rects = new Dictionary
32
33 Texture2D texture = Resources.Load
34 float height = texture.height;
35 TextAsset xml = Resources.Load
36
37 XmlDocument test = new XmlDocument();
38 test.Load(new StringReader(xml.text));
39
40 //test.LoadXml(new StringReader(xml.text).ReadToEnd());
41 //string[] keys = new string[] {"x","y","width","height","yoffset","xadvance","letter"};
42
43 foreach (XmlNode node in test.DocumentElement.ChildNodes)
44 {
45 XmlAttributeCollection collection = node.Attributes;
46 Rect rect = new Rect(float.Parse(collection.Item(0).Value), height - float.Parse(collection.Item(1).Value) - float.Parse(collection.Item(3).Value), float.Parse(collection.Item(2).Value), float.Parse(collection.Item(3).Value));
47 rects.Add(collection.Item(6).Value, rect);
48 sprites.Add(collection.Item(6).Value, Sprite.Create(texture, rect, Vector2.zero));
49 yoffsets_xadvances.Add(collection.Item(6).Value, new Vector2(float.Parse(collection.Item(4).Value), float.Parse(collection.Item(5).Value)));
50 }
51 }
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
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 }
Texture2D 124 lượt xem
Gõ tìm kiếm nhanh...