private void CheckMarkerTextureImporter(Texture2D texture) { if (texture == null) return; string textureFilename = AssetDatabase.GetAssetPath(texture.GetInstanceID()); TextureImporter textureImporter = AssetImporter.GetAtPath(textureFilename) as TextureImporter; if (textureImporter != null) { bool needReimport = false; if (textureImporter.mipmapEnabled) { textureImporter.mipmapEnabled = false; needReimport = true; } if (!textureImporter.isReadable) { textureImporter.isReadable = true; needReimport = true; } if (textureImporter.textureFormat != TextureImporterFormat.ARGB32) { textureImporter.textureFormat = TextureImporterFormat.ARGB32; needReimport = true; } if (needReimport) AssetDatabase.ImportAsset(textureFilename, ImportAssetOptions.ForceUpdate); } }
/// <summary> /// Gets the asset path of a texture. /// </summary> /// <returns>The asset path.</returns> /// <param name="texture">Texture.</param> public static string GetAssetPath(Texture2D texture) { if (texture == null) return string.Empty; return AssetDatabase.GetAssetPath(texture.GetInstanceID()); }
public static void FlattenSSBump(Texture2D Normal, bool flipXZ) { if (!flipXZ) { Texture2D SSBumpTex = new Texture2D(Normal.width, Normal.height, TextureFormat.RGB24, true); Color[] SSBumpColor; Color[] NormalColor; Vector4[] Basis = new Vector4[3]; Basis[0] = new Vector4(0.816496580927726f, 0.0f, 0.5773502691896258f, 0.0f); Basis[1] = new Vector4(-0.408248290463863f, 0.7071067811865475f, 0.5773502691896258f, 0.0f); Basis[2] = new Vector4(-0.408248290463863f, -0.7071067811865475f, 0.5773502691896258f, 0.0f); //Make our Normal Texture readible temporarily TextureImporter tempRNMImport = TextureImporter.GetAtPath(AssetDatabase.GetAssetPath(Normal.GetInstanceID())) as TextureImporter; tempRNMImport.textureType = TextureImporterType.Image; tempRNMImport.isReadable = true; AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(Normal.GetInstanceID()), ImportAssetOptions.ForceUpdate); SSBumpColor = Normal.GetPixels(); NormalColor = Normal.GetPixels(); for (int i = 0; i < SSBumpColor.Length; i++) { SSBumpColor[i].r = (DotBasis(UnpackNormal(NormalColor[i]), Basis[0]) + 1) / 2; SSBumpColor[i].g = (DotBasis(UnpackNormal(NormalColor[i]), Basis[1]) + 1) / 2; SSBumpColor[i].b = (DotBasis(UnpackNormal(NormalColor[i]), Basis[2]) + 1) / 2; } SSBumpTex.SetPixels(SSBumpColor); GzCRNMMergeUtil.TextureImportHelper(SSBumpTex, AssetDatabase.GetAssetPath(Normal.GetInstanceID()) + "_SSBump.png"); tempRNMImport.textureType = TextureImporterType.Bump; tempRNMImport.isReadable = false; AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(Normal.GetInstanceID()), ImportAssetOptions.ForceUpdate); } else { Color[] SSBumpColor; TextureImporter tempRNMImport = TextureImporter.GetAtPath(AssetDatabase.GetAssetPath(Normal.GetInstanceID())) as TextureImporter; tempRNMImport.isReadable = true; tempRNMImport.textureFormat = TextureImporterFormat.ARGB32; AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(Normal.GetInstanceID()), ImportAssetOptions.ForceUpdate); SSBumpColor = Normal.GetPixels(); for (int i = 0; i < SSBumpColor.Length; i++) { SSBumpColor[i].a = SSBumpColor[i].b; SSBumpColor[i].b = SSBumpColor[i].g; SSBumpColor[i].g = SSBumpColor[i].a; SSBumpColor[i].a = 0.0f; } Normal.SetPixels(SSBumpColor); GzCRNMMergeUtil.TextureImportHelper(Normal, AssetDatabase.GetAssetPath(Normal.GetInstanceID()) + "_ConvertedSSBump.png"); tempRNMImport.isReadable = false; AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(Normal.GetInstanceID()), ImportAssetOptions.ForceUpdate); } }
public uint RegisterTexture(Texture2D tex) { if (tex == null) return 0; int texID = tex.GetInstanceID(); uint id = GetTextureID(texID); if (id == 0) { id = guid++; texs.Add(texID, id); if ( id > (1 << 9) ) Debug.LogError("Error, Sprite used Texture2D counts greater than 1024. You need Combine some texture to keep it less than 1024"); } return id; }
public void InitAwesomium(int width, int height) { Debug.Log("init awsommium"); this.width = width; this.height = height; m_texture = new Texture2D(width, height, TextureFormat.ARGB32, true); //Get Color[] (pixels) from texture m_pixels = m_texture.GetPixels(0); // Create window handle id - future usage m_TextureID = m_texture.GetInstanceID(); Debug.Log("textID : " + m_TextureID); // assign m_texture to this GUITexture texture gameObject.renderer.material.mainTexture = m_texture; // Create GCHandle - Allocation of m_pixels in memory. m_pixelsHandler = GCHandle.Alloc(m_pixels, GCHandleType.Pinned); AwesomiumWrapper.Init(); AwesomiumWrapper.CreateAwesomiumWebView(m_TextureID, m_pixelsHandler.AddrOfPinnedObject(), width, height, this.SetPixels, this.ApplyTexture); isAwesomiumInit = true; GetComponent<BrowserGUIEvents>().interactive = true; Debug.Log("done init awsommium"); }
IEnumerable<Sprite> GetTextureSprites(Texture2D tex) { var sprites = new List<Sprite>(); string path = AssetDatabase.GetAssetPath(tex.GetInstanceID()); if (string.IsNullOrEmpty(path)) return sprites; TextureImporter ti = AssetImporter.GetAtPath(path) as TextureImporter; if (ti == null) return sprites; foreach (var dat in ti.spritesheet) { var sp = Sprite.Create(tex, dat.rect, dat.pivot, ti.spritePixelsToUnits); sp.name = dat.name; sprites.Add(sp); } return sprites; }
public string SaveTexture(Texture2D tex) { int id = tex.GetInstanceID(); string name = null; if (savecache.TryGetValue(id, out name)) { return name; } #if UNITY_EDITOR name = SaveTextureEditor(tex); savecache[id] = name; return name; #endif if (parser.debug) Debug.Log("SaveTexture" + tex.name); int i = tex.name.LastIndexOf("."); string ext = "png"; if (i < 0) { tex.name = tex.name + ".png"; } else { ext = tex.name.Substring(i + 1); } if (ext == "png") { byte[] bs = tex.EncodeToPNG(); string sha1 = ResLibTool.ComputeHashString(bs); name = sha1 + "." + ext; bufs[name] = bs; } else if (ext == "jpg") { byte[] bs = tex.EncodeToJPG(); string sha1 = ResLibTool.ComputeHashString(bs); name = sha1 + "." + ext; bufs[name] = bs; } else { throw new Exception("不知道文件类型" + ext); } savecache[id] = name; return name; }
public void ExportTexture(Texture2D asset) { string assetPath = AssetDatabase.GetAssetPath(asset.GetInstanceID()); string exportPath = Path.Combine(assets.TextureDir, assetPath.Replace("Assets/", "")); if (!Directory.Exists(Path.GetDirectoryName(exportPath))) { Directory.CreateDirectory(Path.GetDirectoryName(exportPath)); } if (Path.GetExtension(assetPath) == ".png") { File.Copy(assetPath, exportPath, true); } else { if (asset.format == TextureFormat.ARGB32 || asset.format == TextureFormat.RGB24) { File.WriteAllBytes(exportPath, asset.EncodeToPNG()); } else { Color[] texPixels = asset.GetPixels(); Texture2D tex2 = new Texture2D(asset.width, asset.height, TextureFormat.ARGB32, false); tex2.SetPixels(texPixels); File.WriteAllBytes(exportPath, tex2.EncodeToPNG()); } } //Debug.Log("Exported Texture asset to " + exportPath); }
private void SetReadWrite( Texture2D texture,bool readWrite) { // Get the path of the texture string path = AssetDatabase.GetAssetPath( texture.GetInstanceID()); // Get the textureImporter object TextureImporter textureImporter = AssetImporter.GetAtPath( path ) as TextureImporter; // Texture type to advanced textureImporter.textureType = TextureImporterType.Advanced; // Creat a new setting TextureImporterSettings st = new TextureImporterSettings(); textureImporter.ReadTextureSettings(st); // Texture must be in ARgB32 st.textureFormat = TextureImporterFormat.ARGB32; // Set write/read flag st.readable = readWrite; // Import the new setting textureImporter.SetTextureSettings(st); // Update the asset AssetDatabase.ImportAsset(path); }
void Start() { m_Texture = new Texture2D (width, height, TextureFormat.ARGB32, false); if (m_Texture == null) Debug.Log("Texture error."); m_Texture.Apply(); HTMLTexturePlugin.htmlTexture_start(m_Texture.GetInstanceID(), width, height, "www.google.com"); // put the texture on something if (transform == null) Debug.Log("transorm null."); if (transform.GetComponent<Renderer>() == null) Debug.Log("render null."); transform.GetComponent<Renderer>().sharedMaterial.mainTexture = m_Texture; }
void Start() { // Initialize Berkelium UnityBerkelium.init(); // Create the texture that will represent the website (with optional transparency and without mipmaps) TextureFormat texFormat = transparency ? TextureFormat.ARGB32 : TextureFormat.RGB24; m_Texture = new Texture2D (width, height, texFormat, false); // Create the pixel array for the plugin to write into at startup m_Pixels = m_Texture.GetPixels (0); // "pin" the array in memory, so we can pass direct pointer to it's data to the plugin, // without costly marshaling of array of structures. m_PixelsHandle = GCHandle.Alloc(m_Pixels, GCHandleType.Pinned); // Save the texture ID m_TextureID = m_Texture.GetInstanceID(); // Improve rendering at shallow angles m_Texture.filterMode = FilterMode.Trilinear; m_Texture.anisoLevel = 2; // Assign texture to the renderer if (renderer) { renderer.material.mainTexture = m_Texture; // Transparency? if(transparency) renderer.material.shader = Shader.Find("Transparent/Diffuse"); else renderer.material.shader = Shader.Find("Diffuse"); // The texture has to be flipped renderer.material.mainTextureScale = new Vector2(1,-1); } // or gui texture else if (GetComponent(typeof(GUITexture))) { GUITexture gui = GetComponent(typeof(GUITexture)) as GUITexture; gui.texture = m_Texture; } else { Debug.Log("Game object has no renderer or gui texture to assign the generated texture to!"); } // Create new web window UnityBerkelium.Window.create(m_TextureID, m_PixelsHandle.AddrOfPinnedObject(), transparency, width,height, url); print("Created new web window: " + m_TextureID); // Paint callbacks m_setPixelsFunc = new UnityBerkelium.SetPixelsFunc(this.SetPixels); m_applyTextureFunc = new UnityBerkelium.ApplyTextureFunc(this.ApplyTexture); UnityBerkelium.Window.setPaintFunctions(m_TextureID, m_setPixelsFunc, m_applyTextureFunc); // Set the external host callback (for calling Unity functions from javascript) m_externalHostFunc = new UnityBerkelium.ExternalHostFunc(this.onExternalHost); UnityBerkelium.Window.setExternalHostCallback(m_TextureID, m_externalHostFunc); }
public static void SetTextureImporter( Texture2D texture,TextureImporterFormat format, int size,bool readWrite=true, string path=null) { // Get the path of the texture if (string.IsNullOrEmpty(path)){ path = AssetDatabase.GetAssetPath( texture.GetInstanceID()); } // Get the textureImporter object TextureImporter textureImporter = AssetImporter.GetAtPath( path ) as TextureImporter; // Texture type to advanced textureImporter.textureType = TextureImporterType.Advanced; // Creat a new setting TextureImporterSettings st = new TextureImporterSettings(); textureImporter.ReadTextureSettings(st); // Texture must be in ARgB32 st.textureFormat = format; // Set write/read flag st.readable = readWrite; st.maxTextureSize = size; st.mipmapEnabled = false; st.wrapMode = TextureWrapMode.Clamp; // Import the new setting textureImporter.SetTextureSettings(st); // Update the asset AssetDatabase.ImportAsset(path); }
public static void updateTextureAsset(Texture2D texture) { #if UNITY_EDITOR byte[] bytes = texture.EncodeToPNG(); string assetPath= AssetDatabase.GetAssetPath(texture.GetInstanceID()); FileStream file=File.Open(assetPath,FileMode.Create); BinaryWriter bw=new BinaryWriter(file); bw.Write(bytes); file.Close(); #endif }