【问题标题】:Unity C# How do you save color from colour texture as playerprefsUnity C#如何将颜色纹理中的颜色保存为playerprefs
【发布时间】:2021-01-18 10:35:04
【问题描述】:

我正在为我目前从事的游戏项目制作角色创建者。我正在使用 Texture2D 作为颜色选择器来选择头发、皮肤的颜色,例如我可以使用 DonDestroyOnLoad 保存它,但我想知道如何将它保存为 PlayerPrefs。我最初是从教程https://www.youtube.com/watch?v=rKhFYxUNL6A&list=PLiW_iGwxIxj_lMxm1UJeGNYJbqZ828UYx&index=2&t=1045s 中获得代码的。原人说可以用ToHtmlStringRGB和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);
            }

          

        }
    }
}

【问题讨论】:

    标签: c# unity3d colors


    【解决方案1】:

    首先,你需要将颜色转换为字符串并保存到 PlayerPrefs:

    var colorHexCode = ColorUtility.ToHtmlStringRGBA(color);
    PlayerPrefs.SetString("CharacterColor", colorHexCode);
    

    然后,您可以像这样从 PlayerPrefs 中获取颜色的 HTML 字符串值:

    var colorHexCode = PlayerPrefs.GetString("CharacterColor");
    

    并转换为颜色:

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

    【讨论】:

    • 听起来 OP 已经知道这些,但问我们如何/在哪里将其放置在他的代码中 ..the original person said that I could use ToHtmlStringRGB and TryParseHtmlString, but I not sure how to implement it into the script.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-04
    • 1970-01-01
    • 2016-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-31
    相关资源
    最近更新 更多