The Utilities class provides various helper methods for converting Texture2D objects to different formats, such as Sprite, RenderTexture, and Cubemap. These utilities are essential for manipulating and using textures in Unity projects.
Overview
The Utilities class includes methods for converting textures to sprites, render textures, and cubemaps. These conversions are often needed in game development to handle and display images in different ways. This is especially helpful for converting Diffusion model responses into usable game assets.
Class Definition
usingUnityEngine;namespaceSkillfulAI.API{publicstaticclassUtilities { /// <summary> /// Converts a Texture2D to a Sprite. /// </summary> /// <paramname="texture">The Texture2D to convert.</param> /// <returns>A Sprite created from the Texture2D.</returns>publicstaticSpriteTexture2DToSprite(Texture2D texture) {returnSprite.Create(texture,newRect(0,0,texture.width,texture.height),newVector2(0.5f,0.5f)); } /// <summary> /// Converts a Texture2D to a RenderTexture. /// </summary> /// <paramname="texture">The Texture2D to convert.</param> /// <returns>A RenderTexture created from the Texture2D.</returns>publicstaticRenderTextureTexture2DToRenderTexture(Texture2D texture) {RenderTexture renderTexture =newRenderTexture(texture.width,texture.height,0);Graphics.Blit(texture, renderTexture);return renderTexture; } /// <summary> /// Converts a Texture2D to a Cubemap. /// Note: This is a basic conversion and assumes the texture is a cross layout of the cubemap faces. /// </summary> /// <paramname="texture">The Texture2D to convert.</param> /// <paramname="cubemapSize">The size of the cubemap faces.</param> /// <returns>A Cubemap created from the Texture2D.</returns>publicstaticCubemapTexture2DToCubemap(Texture2D texture,int cubemapSize) {Cubemap cubemap =newCubemap(cubemapSize,texture.format,false);Color[] colors =texture.GetPixels();int faceWidth =texture.width/4;int faceHeight =texture.height/3; // Define the face coordinates (in a cross layout)Rect[] faceRects = {newRect(faceWidth *2, faceHeight *1, faceWidth, faceHeight),// Positive XnewRect(faceWidth *0, faceHeight *1, faceWidth, faceHeight),// Negative XnewRect(faceWidth *1, faceHeight *2, faceWidth, faceHeight),// Positive YnewRect(faceWidth *1, faceHeight *0, faceWidth, faceHeight),// Negative YnewRect(faceWidth *1, faceHeight *1, faceWidth, faceHeight),// Positive ZnewRect(faceWidth *3, faceHeight *1, faceWidth, faceHeight) // Negative Z }; // Assign pixels to cubemap facesfor (int i =0; i <6; i++) {Rect faceRect =faceRects[i];Color[] faceColors =newColor[cubemapSize * cubemapSize];for (int y =0; y < cubemapSize; y++) {for (int x =0; x < cubemapSize; x++) {faceColors[y * cubemapSize + x] =texture.GetPixel( (int)(faceRect.x+ (x *faceRect.width/ cubemapSize)), (int)(faceRect.y+ (y *faceRect.height/ cubemapSize)) ); } }cubemap.SetPixels(faceColors, (CubemapFace)i); }cubemap.Apply();return cubemap; } }}
The Utilities class provides essential methods for converting textures into different formats, which can be crucial for rendering and displaying textures in various ways within Unity. These methods facilitate working with sprites, render textures, and cubemaps, enhancing flexibility and functionality in your Unity projects.