Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

[SOLVED]Color Hue Changing(RGB Values) Of Sprite Using C# Script Code?

Discussion in 'Scripting' started by FallenAngelSoftware, Jun 28, 2021.

  1. FallenAngelSoftware

    FallenAngelSoftware

    Joined:
    Aug 12, 2018
    Posts:
    58
    Hi,

    Making some progress...

    How would we modify a sprite's RGB values using C# script code?
    (color hue change I mean)

    Let us know, thanks!

    Jesse

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. static class visualsCoreGlobal
    6. {
    7.     public static Sprite[] Sprites;
    8.     public static GameObject[] SpriteGOs;
    9.     public static SpriteRenderer[] SpriteRenderers;
    10.  
    11.     public static int LoadSprite(int index, string fileName)
    12.     {
    13.         Sprites[index] = Resources.Load<Sprite>(fileName);
    14.         SpriteGOs[index] = new GameObject("Sprites[index]");
    15.         SpriteRenderers[index] = SpriteGOs[index].AddComponent<SpriteRenderer>();
    16.         SpriteRenderers[index].sprite = Sprites[index];
    17.  
    18.         return (1);
    19.     }
    20.  
    21.     public static int LoadAllSprites()
    22.     {
    23.         Sprites = new Sprite[1300];
    24.         SpriteGOs = new GameObject[1300];
    25.         SpriteRenderers = new SpriteRenderer[1300];
    26.  
    27.         LoadSprite(0, "Sprites/BG_ScreenFade");
    28.  
    29.         LoadSprite(1, "Sprites/TS_Logo");
    30.  
    31.  
    32.         return (1);
    33.     }
    34.  
    35.     public static int DrawSprite(int spriteIndex, float screenX, float screenY, float screenZ, float scaleX, float scaleY, float rotation)
    36.     {
    37.         float xPos = (SpriteRenderers[spriteIndex].bounds.size.x) / (640.0f/screenX);
    38.         float yPos = (SpriteRenderers[spriteIndex].bounds.size.y) / (360.0f/screenY);
    39.         SpriteRenderers[spriteIndex].transform.localPosition = new Vector3(xPos, yPos, screenZ);
    40.  
    41.         SpriteRenderers[spriteIndex].transform.localScale = new Vector3(scaleX, scaleY, 1.0f);
    42.  
    43.         SpriteRenderers[spriteIndex].transform.Rotate(Vector3.forward * rotation);
    44.  
    45.  
    46.         return (1);
    47.     }
    48. }
    49.  
    50. public class visualsCore : MonoBehaviour
    51. {
    52.     // Start is called before the first frame update
    53.     void Start()
    54.     {
    55.        
    56.     }
    57.  
    58.     // Update is called once per frame
    59.     void Update()
    60.     {
    61.        
    62.     }
    63. }
    64.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
  3. FallenAngelSoftware

    FallenAngelSoftware

    Joined:
    Aug 12, 2018
    Posts:
    58
    Got it working, thanks!

    SpriteRenderers[spriteIndex].color = new Vector4(red, green, blue, alpha);
     
    Kurt-Dekker likes this.