【原】unity3d實現調色板


本文采用了原始辦法,調用win32api,實現畫板調用。win32調色板API位於Comdlg32.dll下,以下是實現代碼。

放在unity中的plugins文件夾下

using
UnityEngine; using System.Collections; using System; using System.Runtime.InteropServices; [StructLayout(LayoutKind.Sequential,CharSet=CharSet.Auto)] public class CHOOSECOLOR { public Int32 lStructSize; public Int32 hwndOwner; public Int32 hInstance; public Int32 rgbResult; public IntPtr lpCustColors; public Int32 Flags; public Int32 lCustData; public Int32 lpfnHook; public Int32 lpTemplateName; } public class DllTest { [DllImport("comdlg32.dll",CharSet=CharSet.Auto)] public static extern bool ChooseColorA( CHOOSECOLOR pChoosecolor);//對應的win32API public static bool ChooseColorA1(CHOOSECOLOR pChoosecolor) { return ChooseColorA( pChoosecolor); } }

  

 public struct  Color1
{
 public    float r;
    public float g;
    public float b;
    public float a;
}

if
(GUI.Button(new Rect(150,0,100,35),"OpenColor")) { CHOOSECOLOR choosecolor = new CHOOSECOLOR(); choosecolor.lStructSize = Marshal.SizeOf(choosecolor); choosecolor.hwndOwner =0; choosecolor.rgbResult = 0x808080;//顏色轉成int型 choosecolor.lpCustColors = Marshal.AllocCoTaskMem(64); choosecolor.Flags =0x00000002|0x00000001; if( DllTest.ChooseColorA1( choosecolor)) { a=choosecolor.rgbResult;//獲取int型顏色值 rgba由這個值組成 colorStr=Convert.ToString(a,16);//十進制轉化十六進制 每兩個字符代表一個顏色值 順序從左到右依次為RGB
color.b=Convert.ToInt32(colorStr.Substring(0,2),16)/255f;
color.g=Convert.ToInt32(colorStr.Substring(2,2),16)/255f;
color.r=Convert.ToInt32(colorStr.Substring(4,2),16)/255f; } } GUI.Label(new Rect(0,30,500,30),"aaaa:"+colorStr+":::"+colorStr.Substring(0,2)+" : "+colorStr.Substring(2,2)+" : "+colorStr.Substring(4,2)); GUI.Label(new Rect(0,70,500,30),"aaaa:"+colorStr+":::"+Convert.ToInt32(colorStr.Substring(4,2),16)+":"+Convert.ToInt32(colorStr.Substring(2,2),16)+ ":"+Convert.ToInt32(colorStr.Substring(0,2),16)); plane.renderer.material.color=new Color(color.r,color.g,color.b); }
注意:本文只做了RGB 你們可以補充上A哦,特此提醒,這個程序在unity里面會自動嘣,但是發成exe沒事。關於上面win32的對應,可以查詢msdn相關文檔

實現效果:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM