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

Change Color for Material using Anima2D

Discussion in '2D' started by Kiltres, Jul 16, 2018.

  1. Kiltres

    Kiltres

    Joined:
    Oct 1, 2016
    Posts:
    6
    Sorry for posting again but forum says I cant reply to my own Thread twice.

    This seems simple.

    I want to change the Material color that Anima2D uses in its Sprite Mesh Instance, programmically.

    I want the player to Slowly fade as it gets hit. I can change the color from the editor while its running but cant seem to get to the Material color, its like Anima2D locks it or something.

    I have tried renderer.sharedMaterial but it doesn't seem to change it.

    EDIT: I Figured it out, thanks.

    This is my flow:

    public EnemyShaderDamage[] allShaderDamage;

    // Use this for initialization
    void Start()
    {
    theEnemy = GetComponent<Character>();
    allShaderDamage = theEnemy.GetComponentsInChildren<EnemyShaderDamage>();

    }


    public void HurtEnemy(int damageToTake)
    {
    foreach (EnemyShaderDamage sd in allShaderDamage)
    {
    sd.HurtEnemy(damageToTake);
    }
    }

    below is the script that I add to each of my Body parts of the Character:


    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using Anima2D;


    public class EnemyShaderDamage : MonoBehaviour {

    public SpriteMeshInstance spInstance;
    float m_Hue = 0f;
    float m_Saturation = 0.0f;
    float m_Value = 1.0f;
    public float sDecrease;
    public float sIncrease;



    // Use this for initialization
    void Start()
    {
    spInstance = GetComponentInChildren<SpriteMeshInstance>();
    }

    // Update is called once per frame
    void Update()
    {

    }

    public void HurtEnemy(int damageToTake)
    {
    sDecrease = ((damageToTake * 0.65f) / 100);
    m_Value = (m_Value - sDecrease);
    spInstance.m_Color = Color.HSVToRGB(m_Hue, m_Saturation, m_Value);
    }

    public void HealEnemy(int health)
    {
    sIncrease = ((health * 0.85f) / 100);
    if(sIncrease > 1f)
    {
    sIncrease = 1f;
    }
    m_Value = (m_Value + sIncrease);
    if(m_Value > 1f)
    {
    m_Value = 1f;
    }
    spInstance.m_Color = Color.HSVToRGB(m_Hue, m_Saturation, m_Value);
    }

    public void FullHealth()
    {
    m_Value = 1f;
    spInstance.m_Color = Color.HSVToRGB(m_Hue, m_Saturation, m_Value);
    }

    public void Alpha(float achange)
    {
    spInstance.m_Color.a = achange;
    }
    }
     
    Last edited: Feb 26, 2019
  2. NoneyaBiznazz

    NoneyaBiznazz

    Joined:
    Jul 23, 2017
    Posts:
    2
    Wish you had shared your solution :(
     
  3. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @NoneyaBiznazz

    Yep. Expecting free answer. Begs for help. Solves problem him/herself... *vanishes*
     
  4. ManiaCCC

    ManiaCCC

    Joined:
    Aug 15, 2015
    Posts:
    41
  5. IlisanVlad

    IlisanVlad

    Joined:
    Dec 3, 2017
    Posts:
    30
    You can acces the sprite mesh instance component using this
    Code (CSharp):
    1. GetComponent<Anima2D.SpriteMeshInstance>()
     
  6. Kiltres

    Kiltres

    Joined:
    Oct 1, 2016
    Posts:
    6
    I edited my original post with my code: send a message if you need more info.

    Post was 6 months old and no one posted to it so I figured everyone already new the answer.


    @NoneyaBiznazz @eses
     
    Last edited: Feb 26, 2019
  7. Nivbot

    Nivbot

    Joined:
    Mar 21, 2015
    Posts:
    65
    I’d like to know how to change the damn material. I’ve tried everything it seems.