[Unity C#] The Shader in SecantRemix

[Unity C#] The Shader in SecantRemix

Here is the link to the game – SecantRemix:
https://turist.itch.io/secant-remix

Here is the tutorial of replacement shaders:

Here is the shader we used in the game:

Shader "Hidden/Show Depth"
{
    Properties
    {
        _Color("Color", Color) = (1,1,1,1)
    }

    SubShader
    {
        Tags
        {
            "RenderType"="Opaque"
        }

        ZWrite On

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
            };

            struct v2f
            {
                float4 vertex : SV_POSITION;
                float depth : DEPTH;
            };

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
                o.depth = -mul(UNITY_MATRIX_MV, v.vertex).z *_ProjectionParams.w;
                return o;
            }

            half4 _Color;

            half4 _StartColor;
            half4 _MidColor;
            half4 _EndColor;

            fixed4 frag (v2f i) : SV_Target
            {
                return (max (0, 1 - i.depth * 2) * _StartColor + (0.5 - abs (0.5 - i.depth)) * 2 * _MidColor + max (0, i.depth * 2 - 1) * _EndColor) * _Color;
            }
            ENDCG
        }
    }
}

Also, you need to attach this script to your camera:

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

[ExecuteInEditMode]
public class ReplacementShaderEffect : MonoBehaviour {
    [SerializeField] Shader ReplacementShader; // the shader above
    [SerializeField] Color startColor;
    [SerializeField] Color midColor;
    [SerializeField] Color endColor;

    Camera mainCam;

    private Color startColorTarget;
    private Color midColorTarget;
    private Color endColorTarget;
    [SerializeField] float myColorChangeSpeed = 1;

    void Start() {
        startColorTarget = startColor;
        midColorTarget = midColor;
        endColorTarget = endColor;

        mainCam = GetComponent ();
        Shader.SetGlobalColor("_StartColor", startColor);
        Shader.SetGlobalColor("_MidColor", midColor);
        Shader.SetGlobalColor("_EndColor", endColor);

    }

    void Update() { // make background color always the same as end color in shader

        mainCam.backgroundColor = Color.Lerp (endColor, startColorTarget, Time.deltaTime * myColorChangeSpeed);

        startColor = Color.Lerp (startColor, startColorTarget, Time.deltaTime * myColorChangeSpeed);
        midColor = Color.Lerp (midColor, midColorTarget, Time.deltaTime * myColorChangeSpeed);
        endColor = Color.Lerp (endColor, endColorTarget, Time.deltaTime * myColorChangeSpeed);

        Shader.SetGlobalColor("_StartColor", startColor);
        Shader.SetGlobalColor("_MidColor", midColor);
        Shader.SetGlobalColor("_EndColor", endColor);
    }

    void OnValidate() {
        Shader.SetGlobalColor("_StartColor", startColor);
        Shader.SetGlobalColor("_MidColor", midColor);
        Shader.SetGlobalColor("_EndColor", endColor);

    }

    void OnEnable() {
        if (ReplacementShader != null)
            GetComponent().SetReplacementShader(ReplacementShader, "RenderType");
    }

    void OnDisable() {
        GetComponent().ResetReplacementShader();
    }

    void ChangeStartColor (Color newStartColor) {
        startColorTarget = newStartColor;
    }

    void ChangeMidColor (Color newMidColor) {
        midColorTarget = newMidColor;
    }

    void ChangeEndColor (Color newEndColor) {
        endColorTarget = newEndColor;
    }

    public void ChangeColors (Color g_start, Color g_mid, Color g_end) {
        ChangeStartColor (g_start);
        ChangeMidColor (g_mid);
        ChangeEndColor (g_end);
        Debug.Log ("ChangeColors");
    }

}

Here is the result:

1

[Unity C#] Single Instance

When there should be only one instance of this class in the whole game that shouldn’t be destroyed.

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

public class CS_MessageBox : MonoBehaviour {

    private static CS_MessageBox instance = null;

    //========================================================================
    public static CS_MessageBox Instance {
        get { 
            return instance;
        }
    }

    void Awake () {
        if (instance != null && instance != this) {
            Destroy(this.gameObject);
        } else {
            instance = this;
        }
        DontDestroyOnLoad(this.gameObject);
    }
    //========================================================================

    public void FunctionYouNeed () {
        Debug.Log("FunctionYouNeed is called :)");
    }
}

To call this instance

CS_MessageBox.Instance.FunctionYouNeed();

[Unity C#] Serializable Class

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

[System.Serializable]
public class CS_AudioClip {
    public AudioClip[] myAudioClips;
    public float myVolum = 1;
    public float myPitchDifference = 0;

    public AudioClip GetMyAudioClip () {
        if (myAudioClips.Length <= 0)
            return null;
        return myAudioClips [Random.Range (0, myAudioClips.Length)];
    }

    public float GetMyPitch () {
        return (1 + myPitchDifference * Random.Range (0, 1.0f));
    }
}

to use this class in other scripts:

public CS_AudioClip myMoveSFX;