DrawingMap.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. public class DrawingMap : MonoBehaviour
  6. {
  7. [SerializeField]
  8. private Image colorImage;//颜色池
  9. private Texture2D temp_texture;
  10. void Start()
  11. {
  12. InitColor();
  13. }
  14. // Update is called once per frame
  15. void Update()
  16. {
  17. }
  18. private void InitColor()
  19. {
  20. temp_texture = this.colorImage.mainTexture as Texture2D;
  21. //CDebug.Log("texture.height" + texture.height + "texture.width" + texture.width);
  22. int addValue = (temp_texture.width - 1) / 3;
  23. Color value0 = (Color.green - Color.red) / addValue;
  24. Color value1 = (Color.blue - Color.green) / addValue;
  25. Color value2 = (Color.red - Color.blue) / addValue;
  26. for (int x = 0; x < temp_texture.width; x++)
  27. {
  28. for (int y = 0; y < temp_texture.height; y++)
  29. {
  30. if (x < addValue)
  31. {
  32. temp_texture.SetPixel(x, y, Color.red + value0 * x);
  33. }
  34. else if (x < addValue * 2)
  35. {
  36. temp_texture.SetPixel(x, y, Color.green + value1 * (x - addValue));
  37. }
  38. else
  39. {
  40. temp_texture.SetPixel(x, y, Color.blue + value2 * (x - addValue * 2));
  41. }
  42. }
  43. }
  44. // this.colorImage.sprite = Sprite.Create(temp_texture, new Rect(0, 0, 256f,128f), Vector2.zero);
  45. temp_texture.Apply();
  46. // this.colorImage.material.mainTexture = temp_texture;
  47. }
  48. public Color GetColorByPoint(Vector2 point)
  49. {
  50. return temp_texture.GetPixel((int) point.x, (int)point.y);
  51. }
  52. }