Unity C#如何将颜色纹理中的颜色保存为播放器参考

Unity C#如何将颜色纹理中的颜色保存为播放器参考,c#,unity3d,colors,C#,Unity3d,Colors,我正在为我目前正在从事的一个游戏项目制作一个角色创建者。我正在使用Texture2D作为颜色选择器来拾取头发、皮肤的颜色,例如,我可以使用DonDestroyOnLoad保存它,但我想知道如何将其保存为PlayerPrefs。我最初是从一个教程中获得代码的。最初的人说我可以使用htmlstringrgb和TryParseHtmlString,但我不知道如何在脚本中实现它 using System.Collections; using System.Collections.Generic; usi

我正在为我目前正在从事的一个游戏项目制作一个角色创建者。我正在使用Texture2D作为颜色选择器来拾取头发、皮肤的颜色,例如,我可以使用DonDestroyOnLoad保存它,但我想知道如何将其保存为PlayerPrefs。我最初是从一个教程中获得代码的。最初的人说我可以使用htmlstringrgb和TryParseHtmlString,但我不知道如何在脚本中实现它

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using System;
using UnityEngine.Events;

[Serializable]
public class ColorEvent : UnityEvent<Color> { }
public class ColourPicker : MonoBehaviour
{
    public TextMeshProUGUI DebugText;
    public ColorEvent OnColorPreview;
    public ColorEvent OnColorSelect;
    RectTransform Rect;
    Texture2D ColorTexture;
  
    void Start()
    {
        Rect = GetComponent<RectTransform>();

        ColorTexture = GetComponent<Image>().mainTexture as Texture2D;
    }
   
    void Update()
    {
        if (RectTransformUtility.RectangleContainsScreenPoint(Rect, Input.mousePosition))
        {
            Vector2 delta;
            RectTransformUtility.ScreenPointToLocalPointInRectangle(Rect, Input.mousePosition, null, out delta);

            string debug = "mousePosition=" + Input.mousePosition;
            debug += "<br>delta" + delta;

            float width = Rect.rect.width;
            float height = Rect.rect.height;
            delta += new Vector2(width * .5f, height * .5f);
            debug += "<br>offset delta" + delta;

            float x = Mathf.Clamp(delta.x / width, 0f, 1f);
            float y = Mathf.Clamp(delta.y / height, 0f, 1f);
            debug += "<br>x=" + x + " y=" + y;

            int texX = Mathf.RoundToInt(x * ColorTexture.width);
            int texY = Mathf.RoundToInt(y * ColorTexture.height);
            debug += "<br>texX=" + texX + " texY=" + texY;

            Color color = ColorTexture.GetPixel(texX, texY);
            DebugText.color = color;
            DebugText.text = debug;


            OnColorPreview?.Invoke(color);

            if (Input.GetMouseButtonDown(0))
            {
                OnColorSelect?.Invoke(color);
            }

          

        }
    }
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用UnityEngine.UI;
使用TMPro;
使用制度;
使用UnityEngine.Events;
[可序列化]
公共类ColorEvent:UnityEvent{}
公共类色彩采集器:MonoBehavior
{
公共文本mesh prougui DebugText;
公共色彩事件;
公共颜色事件OnColorSelect;
Rect变换Rect;
纹理2d彩色纹理;
void Start()
{
Rect=GetComponent();
ColorTexture=GetComponent().mainTexture作为Texture2D;
}
无效更新()
{
if(RectTransformUtility.RectangleContainsScreenPoint(Rect,Input.mousePosition))
{
矢量2三角洲;
RectTransformUtility.ScreenPointToLocalPointInRectangle(Rect,Input.mousePosition,null,out delta);
string debug=“mousePosition=“+Input.mousePosition;
调试+=“
增量”+增量; 浮动宽度=Rect.Rect.width; 浮动高度=Rect.Rect.height; 增量+=新矢量2(宽度*.5f,高度*.5f); 调试+=“
偏移增量”+增量; 浮动x=数学夹具(增量x/宽度,0f,1f); 浮动y=主夹具(增量y/高度,0f,1f); 调试+=”
x=“+x+”y=“+y; int texX=Mathf.RoundToInt(x*ColorTexture.width); int-texY=Mathf.RoundToInt(y*颜色纹理.高度); 调试+=”
texX=“+texX+”texY=“+texY; Color Color=ColorTexture.GetPixel(texX,texY); DebugText.color=颜色; DebugText.text=debug; OnColorPreview?.Invoke(颜色); if(Input.GetMouseButtonDown(0)) { OnColorSelect?.Invoke(颜色); } } } }
首先,您需要将颜色转换为字符串并保存到PlayerPrefs:

var colorHexCode = ColorUtility.ToHtmlStringRGBA(color);
PlayerPrefs.SetString("CharacterColor", colorHexCode);
然后,您可以从PlayerPref中获取颜色的HTML字符串值,如下所示:

var colorHexCode = PlayerPrefs.GetString("CharacterColor");
并转换为颜色:

Color color = Color.black;
if(ColorUtility.TryParseHtmlString(colorHexCode, out color))
{
  // Do something if color parsing is successful
}

听起来OP已经知道这些,但问我们如何/在何处将其放入他的代码中<代码>最初的人说我可以使用HTMLSTRINGRGB和TryParseHtmlString,但我不知道如何在脚本中实现它。