├── README.md ├── SwapTwo.shader └── SwappedTextures.cs /README.md: -------------------------------------------------------------------------------- 1 | # UnityAnimatorChangeSprite 2 | This script will swap sprite of an active animator to another one with the same dimensions. 3 | 4 | Just add this script to a gameobject with animator and change "swappedTexture" through script or in editor and place swappedTexture.shader into Resources/Shaders folder. Afterwards you can swap texture through the script with a SwapTexture(Texture2D _toWhat) function. 5 | This will make animation of characters with different clothes way easier. You can animate main character and swap his clothes on the fly 6 | -------------------------------------------------------------------------------- /SwapTwo.shader: -------------------------------------------------------------------------------- 1 | Shader "Custom/SwapTwo" { 2 | Properties 3 | { 4 | [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {} 5 | _MainTex2 ("_MainTex2", 2D) = "white" {} 6 | _Color ("Tint", Color) = (1,1,1,1) 7 | [MaterialToggle] PixelSnap ("Pixel snap", Float) = 0 8 | } 9 | 10 | SubShader 11 | { 12 | Tags 13 | { 14 | "Queue"="Transparent" 15 | "IgnoreProjector"="True" 16 | "RenderType"="Transparent" 17 | "PreviewType"="Plane" 18 | "CanUseSpriteAtlas"="True" 19 | } 20 | 21 | Cull Off 22 | Lighting Off 23 | ZWrite Off 24 | Blend One OneMinusSrcAlpha 25 | 26 | CGPROGRAM 27 | #pragma surface surf Lambert vertex:vert nofog keepalpha 28 | #pragma multi_compile _ PIXELSNAP_ON 29 | #pragma multi_compile _ ETC1_EXTERNAL_ALPHA 30 | 31 | sampler2D _MainTex; 32 | fixed4 _Color; 33 | sampler2D _MainTex2; 34 | 35 | struct Input 36 | { 37 | float2 uv_MainTex; 38 | fixed4 color; 39 | }; 40 | 41 | void vert (inout appdata_full v, out Input o) 42 | { 43 | #if defined(PIXELSNAP_ON) 44 | v.vertex = UnityPixelSnap (v.vertex); 45 | #endif 46 | 47 | UNITY_INITIALIZE_OUTPUT(Input, o); 48 | o.color = v.color * _Color; 49 | } 50 | 51 | void surf (Input IN, inout SurfaceOutput o) 52 | { 53 | half4 c = tex2D (_MainTex2, IN.uv_MainTex) * IN.color; 54 | 55 | o.Albedo = c.rgb * c.a; 56 | o.Alpha = c.a; 57 | } 58 | ENDCG 59 | } 60 | 61 | Fallback "Transparent/VertexLit" 62 | } -------------------------------------------------------------------------------- /SwappedTextures.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class SwappedTextures : MonoBehaviour { 6 | public Texture2D swappedTexture = null; 7 | 8 | private SpriteRenderer thisRenderer; 9 | private void Awake() 10 | { 11 | thisRenderer = GetComponent(); 12 | if (thisRenderer) 13 | { 14 | Shader _swapShader = Shader.Find("Custom/SwapTwo"); 15 | if (!_swapShader) 16 | { 17 | Debug.LogError("You dont have shader... "); 18 | } 19 | else 20 | { 21 | Material _newMat = new Material(_swapShader); 22 | thisRenderer.material = _newMat; 23 | thisRenderer.material.SetTexture("_MainTex2", swappedTexture); 24 | } 25 | } 26 | else 27 | { 28 | Debug.LogError("There is NO spriterenderer attached to gameobject " + this.name); 29 | } 30 | } 31 | 32 | /// 33 | /// this will swap our used animator texture to another one 34 | /// 35 | /// 36 | public void SwapTexture(Texture2D _toWhat) 37 | { 38 | swappedTexture = _toWhat; 39 | if (thisRenderer) 40 | { 41 | Shader _swapShader = Shader.Find("Custom/SwapTwo"); 42 | if (!_swapShader) 43 | { 44 | Debug.LogError("You dont have shader... "); 45 | } 46 | else 47 | { 48 | Material _newMat = new Material(_swapShader); 49 | thisRenderer.material = _newMat; 50 | thisRenderer.material.SetTexture("_MainTex2", swappedTexture); 51 | } 52 | } 53 | else 54 | { 55 | Debug.LogError("There is NO spriterenderer attached to gameobject " + this.name); 56 | } 57 | } 58 | 59 | public void Update() 60 | { 61 | if (swappedTexture != null) 62 | { 63 | SwapTexture(swappedTexture); 64 | swappedTexture = null; 65 | } 66 | } 67 | } 68 | --------------------------------------------------------------------------------