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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Resolved Changing Color in Scripting? (Synty Package)

Discussion in 'Scripting' started by StephCat, Jun 9, 2020.

  1. StephCat

    StephCat

    Joined:
    Nov 17, 2016
    Posts:
    20
    I am using a Synty polygon Modular character package, and I want to be able to change the color of a player's skin, hair, etc in a character creator. The package comes with a character randomizer script, but even in a fresh project it does not change any colors (Synty says it works fine on their end... so it could be some weird setup issue i guess?).

    I've tried getting a mesh renderer from an object, setting material color using SetColor... nothing works except going into the individual object and changing the albedo manually during run time or switching out the material entirely manually which obviously wont work for a player. I've tried straightforward code like:

    Code (CSharp):
    1. //mat is a the public material of the code
    2. //hair [] is a color array          
    3. mat.SetColor("_Color_Hair", hair[Random.Range(0, hair.Length)]);
    just to see if it would do something.

    I've also tried:
    Code (CSharp):
    1.     public void ChangeHairColor()
    2.     {
    3.          //enabled objects is an object array that tracks what pieces of modular character are visible
    4.         for (int i = 0; i < enabledObjects.Count; i++)
    5.         {
    6.             for (int j = 0; j < allGender.all_Hair.Count; j++)
    7.             {
    8.                 if (allGender.all_Hair[j].name == enabledObjects[i].name)
    9.                 {
    10.                     Debug.Log("Match found at " + j);
    11.  
    12.                     var hairMaterial = enabledObjects[i].GetComponent<Material>();
    13.                     hairMaterial.SetColor("_Color_Hair", blackHair[0]);
    14.                     //exit loops
    15.                     j = allGender.all_Hair.Count - 1;
    16.                     i = enabledObjects.Count - 1;
    17.                 }
    18.             }
    19.         }
    20.     }
    To see if i needed to get the material from the individual object or something.

    In case i'm barking up the wrong tree or it's a settings issue, the player is set up with every possible customization piece preattached and it uses this fancy shader
    upload_2020-6-9_14-4-49.png
    upload_2020-6-9_14-6-4.png
    upload_2020-6-9_14-6-27.png

    the individual pieces are set up similar to this: with a skin mesh renderer and I think a material (it says texture in the name so i get a bit confused)?

    upload_2020-6-9_14-12-43.png
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    So you're saying what Synty is doing to randomize characters does not affect the character at your desk?
     
  3. StephCat

    StephCat

    Joined:
    Nov 17, 2016
    Posts:
    20
    Yes, it will randomize all of the pieces but it will not change any colors. Synty said that the script was fan made and only works on some versions of Unity, but that they tested it on the same version of unity that I'm using (2019.3.5f1) and that it works on their end. But I've imported their package fresh into a blank project and still can't get the results that they talk about where the colors change
     
  4. StephCat

    StephCat

    Joined:
    Nov 17, 2016
    Posts:
    20
    If you want to know how the original script did it (after taking out the lines of code not related to one color aspect... like hair color):
    Code (CSharp):
    1. // character randomizer version 1.30
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. namespace PsychoticLab
    6. {
    7.     public enum Gender { Male, Female }
    8.     public enum Race { Human, Elf }
    9.     public enum SkinColor { White, Brown, Black, Elf }
    10.     public enum Elements { Yes, No }
    11.     public enum HeadCovering { HeadCoverings_Base_Hair, HeadCoverings_No_FacialHair, HeadCoverings_No_Hair }
    12.     public enum FacialHair { Yes, No }
    13.  
    14.     public class CharacterRandomizer : MonoBehaviour
    15.     {
    16.         [Header("Material")]
    17.         public Material mat;
    18.  
    19.         [Header("Hair Colors")]
    20.         public Color[] blackHair = { new Color(0.2431373f, 0.2039216f, 0.145098f), new Color(0.1764706f, 0.1686275f, 0.1686275f), new Color(0.1764706f, 0.1686275f, 0.1686275f) };
    21.  
    22.         // list of enabed objects on character
    23.         [HideInInspector]
    24.         public List<GameObject> enabledObjects = new List<GameObject>();
    25.  
    26.         // character object lists
    27.         // male list
    28.         [HideInInspector]
    29.         public CharacterObjectGroups male;
    30.  
    31.         // female list
    32.         [HideInInspector]
    33.         public CharacterObjectGroups female;
    34.  
    35.         // universal list
    36.         [HideInInspector]
    37.         public CharacterObjectListsAllGender allGender;
    38.      
    39.  
    40.         private void Start()
    41.         {
    42.             // rebuild all lists
    43.             BuildLists();
    44.  
    45.             // disable any enabled objects before clear
    46.             if (enabledObjects.Count != 0)
    47.             {
    48.                 foreach (GameObject g in enabledObjects)
    49.                 {
    50.                     g.SetActive(false);
    51.                 }
    52.             }
    53.  
    54.             // clear enabled objects list
    55.             enabledObjects.Clear();
    56.  
    57.             // set default male character
    58.             ActivateItem(allGender.all_Hair[0]);
    59.         }
    60.  
    61.         }
    62.  
    63.         void RandomizeAndSetHairSkinColors(string info, Color[] skin, Color[] hair, Color stubble, Color scar)
    64.         {
    65.             // randomize and set elf hair color
    66.             if (hair.Length != 0)
    67.             {
    68.                 mat.SetColor("_Color_Hair", hair[Random.Range(0, hair.Length)]);
    69.             }
    70.             else
    71.             {
    72.                 Debug.Log("No " + info + " Hair Colors Specified In The Inspector");
    73.             }
    74.  
    75.         }
    76.         // enable game object and add it to the enabled objects list
    77.         void ActivateItem(GameObject go)
    78.         {
    79.             // enable item
    80.             go.SetActive(true);
    81.  
    82.             // add item to the enabled items list
    83.             enabledObjects.Add(go);
    84.         }
    85.  
    86.         Color ConvertColor(int r, int g, int b)
    87.         {
    88.             return new Color(r / 255.0f, g / 255.0f, b / 255.0f, 1);
    89.         }
    90.  
    91.         // build all item lists for use in randomization
    92.         private void BuildLists()
    93.         {
    94.             // build out all gender lists
    95.             BuildList(allGender.all_Hair, "All_01_Hair");
    96.         }
    97.  
    98.         // called from the BuildLists method
    99.         void BuildList(List<GameObject> targetList, string characterPart)
    100.         {
    101.             Transform[] rootTransform = gameObject.GetComponentsInChildren<Transform>();
    102.  
    103.             // declare target root transform
    104.             Transform targetRoot = null;
    105.  
    106.             // find character parts parent object in the scene
    107.             foreach (Transform t in rootTransform)
    108.             {
    109.                 if (t.gameObject.name == characterPart)
    110.                 {
    111.                     targetRoot = t;
    112.                     break;
    113.                 }
    114.             }
    115.  
    116.             // clears targeted list of all objects
    117.             targetList.Clear();
    118.  
    119.             // cycle through all child objects of the parent object
    120.             for (int i = 0; i < targetRoot.childCount; i++)
    121.             {
    122.                 // get child gameobject index i
    123.                 GameObject go = targetRoot.GetChild(i).gameObject;
    124.  
    125.                 // disable child object
    126.                 go.SetActive(false);
    127.  
    128.                 // add object to the targeted object list
    129.                 targetList.Add(go);
    130.  
    131.                 // collect the material for the random character, only if null in the inspector;
    132.                 if (!mat)
    133.                 {
    134.                     if (go.GetComponent<SkinnedMeshRenderer>())
    135.                         mat = go.GetComponent<SkinnedMeshRenderer>().material;
    136.                 }
    137.             }
    138.         }
    139.     }
    140.  
    141.     // classe for keeping the lists organized, allows for organization of the all gender items
    142.     [System.Serializable]
    143.     public class CharacterObjectListsAllGender
    144.     {
    145.         public List<GameObject> all_Hair;
    146.     }
    147. }
    148.  
     
  5. StephCat

    StephCat

    Joined:
    Nov 17, 2016
    Posts:
    20
    I figured out finally what the issue was and wanted to share the news with anyone who might need this later. So, this synty package comes with multiple character prefabs. Some are in a folder where they only have one of each piece showing (useful for NPCs if you wanna get something in quick with no finagling on your part) and theres one called "ModularCharacters" that outside of these folders and has every possible piece activated. This one with everything activated is what I used for my code, and the reason why the colors wouldn't change was because for some reason, the material on each piece in this ModularCharacters prefab is different from the material in the other presets. The code works fine when replacing the character graphic with any one of the presets that comes in the folder.
     
    Kurt-Dekker likes this.
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    Ah, that makes perfect sense now. That different material is the one that respects those property tweaks such as setting Hair Color, etc.

    Thanks for the followup.