需求如下:要打包后的exe文件可以实现给地板换颜色,而且必须用取色器来选颜色,当下次打开的时候颜色还要保留成上次选择后的颜色。
思路:
unity自带的取色器只能在编辑状态用,打包之后是用不了的,所以只能调用windows自己的取色器。下次打开软件要加载上次保留的颜色信息,也就是颜色是需要保存和加载的,方式就是保存颜色的RGB的数值就OK。
实现效果:
![换色功能](/i/li/?n=2&i=images/blog/202301/05124158_63b655162c7e240505.gif)
实现方法:
首先我们得调用windows的代码;将以下代码放到unity的Plugins里
将绑定到plane上运行即可。
using UnityEngine;
using System.Collections;
using System.IO;
using System;
using System.Runtime.InteropServices;
using System.Text;
public class changecc : MonoBehaviour
{
private Color color;
private int a;
private string colorStr;
public GameObject plane;
string path = Environment.CurrentDirectory;
// Use this for initialization
void Start()
{
Debug.Log(path);
//读取文本里的RGB值
using (FileStream file = new FileStream(path + @"\1.txt", FileMode.OpenOrCreate, FileAccess.Read))
{
byte[] buffer = new byte[1024 * 1024 * 2];
int i = file.Read(buffer, 0, buffer.Length);
string s = Encoding.UTF8.GetString(buffer, 0, i);
string[]sarry= s.Split(',');
float bb;
float.TryParse( sarry[a],out bb);
color.r = bb;
color.g =float.Parse( sarry[a+1]);
color.b =float.Parse( sarry[a+2].ToString());
Debug.Log(sarry[a] + "\n");
Debug.Log(color.r + "\n"+color.g +"\n"+ color.b);
// plane.GetComponent<Renderer>().material.color = new Color(color.r / 255f, color.g / 255f, color.b / 255f);//unity的RGB值要除以255
plane.GetComponent<Renderer>().material.color = new Color(color.r , color.g, color.b);
}
}
[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);
}
}
private void OnGUI()
{
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;
}
//储存颜色的rgb值到文本里
using (FileStream writecolor = new FileStream(path + @"\1.txt", FileMode.OpenOrCreate, FileAccess.Write))
{
string save = color.r.ToString() + "," + color.g.ToString() + "," + color.b.ToString();
byte[] savesome = new byte[1024 * 1024 * 2];
savesome = Encoding.Default.GetBytes(save);
writecolor.Write(savesome, 0, save.Length);
}
//改变plane的颜色
plane.GetComponent<Renderer>().material.color = new Color(color.r, color.g, color.b);
}
}
}
解释代码:
首先在start里将存储RGB值的文本读取,然后存储的格式是用逗号隔开R,G,B三个数值,当读取之后给plane赋值。下面是RGB的存储格式截图图片:
在下面是使用OnGUI的方法,当按钮点击的时候就调出windows的调色板选色,然后吧选好的颜色存储到上面提到的文本里。之后再改变plane的rgb值。
这样就完成了!
标签:Int32,windows,unity,color,取色,using,new,public,choosecolor From: https://blog.51cto.com/u_8378185/5990719