[Unity] 유니티 스크립트_2D그림자 만들기/Script _create 2d sprite animated shadow_excuteeditormode

2019. 8. 8. 11:47unity/unity script

our project use sprite image

like this

 

Sprite image shadows usually use a circle image, but the team wanted the shadows to animate

But since my project's drawcall was already full, using more images is not good for our game performance

 

my idea is created fake shadow 

 

my project is summoning many 'characer',  do each character's quality is not important
point is how good feels on screen(acting and animating)

 

howevere, we faced some probolome after decide take that direction 'animating shadow following main character'

we increase no more drawcall

 

look this image

shadow on this image is animating

 

it's simple fake 

same material same mesh does not increase drawcall

if so, the shadow sprites had to get the materials and sprites from the parent sprites

and just make the colors look like black

 

The above image is copied parents material and sprite, and change color

 

on image's  up inspector is main sprite, down is shadow

that script is creating instance shadow plane with parents materials and sprite

after that, change shadow panel's vertex color on sprite renderer

 

before that,  prepare the parents material able to applied vertex color input 

 

now show script code

 

this script is run on editormode 

so, not enough optimizing

 

 

////////////////////////////////////// createshadow.cs

 

using UnityEngine;


[ExecuteInEditMode] //<< this scripts run on editor mode


public class Create_shadow : MonoBehaviour
{
    public Material setMemberInput;
    public Vector3 shadowPos = new Vector3(0f, -0.53f, 0.17f);
    public Vector3 shadowangle = new Vector3(147f, 0f, 0f);
    public Vector3 shadowScale = new Vector3(1f, 0.5f, 1f);
    private Transform findG;
//<<set variables

//<<The reason all variables are declared 'public' is because the current step is development

// << The artist sets the parameter by looking at the variable and game scene

      looks like how?



    void Update()
    {
        var find = gameObject.transform.Find("shadow_ins");
//<<this line is only use in editor mode, don't need to check it in real game scripts

/ << create shadow plane if result doesn't have shadow panel, otherwise update sprite image
        if (find == null) /<<this block is said about creating shadow sprite
        {
            var gameObject = new GameObject("shadow_ins"); /<<prepare variable for create sprite plane

            gameObject.transform.parent = this.gameObject.transform;
            var addComponent = gameObject.AddComponent(typeof(SpriteRenderer));

/<<'add sprite renderer component to gameobject'


            var shadowRender = addComponent.GetComponent();

/<<connet to new sprite renderer component(named 'addcomponent')

 

            shadowRender.transform.localPosition = shadowPos;
            shadowRender.transform.localEulerAngles = shadowangle;
            shadowRender.transform.localScale = shadowScale;

/<<set shadow plane's transform from prepared variables


            setMemberInput = this.GetComponent().sharedMaterial;

/<< material instantiation with the 'sharedmaterial' command
Be careful not to use the .material command
.material command will be increasing drawcall

            shadowRender.material = setMemberInput;
            shadowRender.color = new Color(0f, 0f, 0f, 0.5f);

/<< aplly material and color to sprite renderer

/<<  focus change color is not material color, it is sprite.color, 

 

if you change material color is increasing drawcall
the reason is any changing material parameter  action is creating other materials(instancing)

 

actually, unity served default sprite material is  able receiving input vertexcolor
and sprite renderer's color change meaning change vertexcolor

 

Extending this fact you get 4 parameters (rgba) that you can use without increasing drawcall.
Also, if you have a parameter that can be automatically received 
when creating a material

(normal or other parameters that can be changed without access to the material),

 

 you can express many things without increasing draw calls

 



        }
        else
        {
            var getComponent = gameObject.GetComponent().GetComponent();
            var getComponent2 = find.GetComponent();

            getComponent2.sprite = getComponent.sprite;

 

/<< update sprite from main object if already shadow creted

 

        }

    }
}

 

//////////////////////////////////////////////////////////

 

The best quality is crafting the real shadow, but the solution leads very heavy calculations

This solution provides a medium quality screen that looks like something is animated(or moving)

 

if your game is sponed many character and battle on same time(in chaotic screen)
this solution will be upgrading whole view quality