Anim
How do I use Anim
Below are practical examples compiled from projects for learning and reference purposes
Featured Snippets
File name: JumpAndRunMovement.cs
Copy
15 void Awake()
16 {
17 m_Animator = GetComponent
18 m_Body = GetComponent
19 m_PhotonView = GetComponent
20 }
File name: JumpAndRunMovement.cs
Copy
52 void UpdateJumping()
53 {
54 if( Input.GetKey( KeyCode.Space ) == true && m_IsGrounded == true )
55 {
56 m_Animator.SetTrigger( "IsJumping" );
57 m_Body.AddForce( Vector2.up * JumpForce );
58 m_PhotonView.RPC( "DoJump", PhotonTargets.Others );
59 }
60 }
File name: JumpAndRunMovement.cs
Copy
63 void DoJump()
64 {
65 m_Animator.SetTrigger( "IsJumping" );
66 }
File name: JumpAndRunMovement.cs
Copy
89 void UpdateIsRunning()
90 {
91 m_Animator.SetBool( "IsRunning", Mathf.Abs( m_Body.velocity.x ) > 0.1f );
92 }
File name: JumpAndRunMovement.cs
Copy
94 void UpdateIsGrounded()
95 {
96 Vector2 position = new Vector2( transform.position.x, transform.position.y );
97
98 RaycastHit2D hit = Physics2D.Raycast( position, -Vector2.up, 0.1f, 1 << LayerMask.NameToLayer( "Ground" ) );
99
100 m_IsGrounded = hit.collider != null;
101 m_Animator.SetBool( "IsGrounded", m_IsGrounded );
102 }
File name: HubGui.cs
Copy
28 void OnGUI()
29 {
30 GUI.skin = this.Skin;
31 GUILayout.Space(10);
32
33 GUILayout.BeginHorizontal();
34 GUILayout.Space(10);
35 scrollPos = GUILayout.BeginScrollView(scrollPos, GUILayout.Width(320));
36
37 GUILayout.Label("Basics", m_Headline);
38 if (GUILayout.Button("Demo Boxes", GUILayout.Width(280)))
39 {
40 demoDescription = "
41 demoBtn = new DemoBtn() { Text = "Start", Link = "DemoBoxes-Scene" };
42 }
43 if (GUILayout.Button("Demo Worker", GUILayout.Width(280)))
44 {
45 demoDescription = "
46 demoBtn = new DemoBtn() { Text = "Start", Link = "DemoWorker-Scene" };
47 }
48 if (GUILayout.Button("Movement Smoothing", GUILayout.Width(280)))
49 {
50 demoDescription = "
51 demoBtn = new DemoBtn() { Text = "Start", Link = "DemoSynchronization-Scene" };
52 }
53
54 GUILayout.Label("Advanced", m_Headline);
55 if (GUILayout.Button("Ownership Transfer", GUILayout.Width(280)))
56 {
57 demoDescription = "
58 this.demoBtn = new DemoBtn() { Text = "Start", Link = "DemoChangeOwner-Scene" };
59 this.webLink = new DemoBtn();
60 }
61 if (GUILayout.Button("Pickup, Teams, Scores", GUILayout.Width(280)))
62 {
63 demoDescription = "
64 this.demoBtn = new DemoBtn() { Text = "Start", Link = "DemoPickup-Scene" };
65 this.webLink = new DemoBtn();
66 }
67
68 GUILayout.Label("Feature Demos", m_Headline);
69 if (GUILayout.Button("Chat", GUILayout.Width(280)))
70 {
71 demoDescription = "
72 this.demoBtn = new DemoBtn() { Text = "Start", Link = "DemoChat-Scene" };
73 this.webLink = new DemoBtn();
74 }
75 if (GUILayout.Button("RPG Movement", GUILayout.Width(280)))
76 {
77 demoDescription = "
78 this.demoBtn = new DemoBtn() { Text = "Start", Link = "DemoRPGMovement-Scene" };
79 this.webLink = new DemoBtn();
80 }
81 if (GUILayout.Button("Mecanim Animations", GUILayout.Width(280)))
82 {
83 demoDescription = "
84 this.demoBtn = new DemoBtn() { Text = "Start", Link = "DemoMecanim-Scene" };
85 this.webLink = new DemoBtn();
86 }
87 if (GUILayout.Button("2D Game", GUILayout.Width(280)))
88 {
89 demoDescription = "
90 this.demoBtn = new DemoBtn() { Text = "Start", Link = "Demo2DJumpAndRunWithPhysics-Scene" };
91 this.webLink = new DemoBtn();
92 }
93 if (GUILayout.Button("Friends & Authentication", GUILayout.Width(280)))
94 {
95 demoDescription = "
96 this.demoBtn = new DemoBtn() { Text = "Start", Link = "DemoFriends-Scene" };
97 this.webLink = new DemoBtn();
98 }
99
100 GUILayout.Label("Tutorial", m_Headline);
101 if (GUILayout.Button("Marco Polo Tutorial", GUILayout.Width(280)))
102 {
103 demoDescription = "
104 this.demoBtn = new DemoBtn() { Text = "Start", Link = "MarcoPolo-Scene" };
105 this.webLink = new DemoBtn() { Text = "Open Tutorial (www)", Link = "http://tinyurl.com/nmylf44" };
106 }
107 GUILayout.EndScrollView();
108
109 GUILayout.BeginVertical(GUILayout.Width(Screen.width - 345));
110 GUILayout.Label(demoDescription);
111 GUILayout.Space(10);
112 if (!string.IsNullOrEmpty(this.demoBtn.Text))
113 {
114 if (GUILayout.Button(this.demoBtn.Text))
115 {
116 Application.LoadLevel(this.demoBtn.Link);
117 }
118 }
119 if (!string.IsNullOrEmpty(this.webLink.Text))
120 {
121 if (GUILayout.Button(this.webLink.Text))
122 {
123 Application.OpenURL(this.webLink.Link);
124 }
125 }
126 GUILayout.EndVertical();
127
128
129 GUILayout.EndHorizontal();
130 }
File name: DemoMecanimGUI.cs
Copy
16 private PhotonAnimatorView m_AnimatorView; // local animatorView. set when we create our character in CreatePlayerObject()
17 private Animator m_RemoteAnimator; // to display the synchronized values on the right side in the GUI. A third player will simply be ignored (until the second player leaves)
28 public void Awake()
29 {
30
31 }
File name: DemoMecanimGUI.cs
Copy
33 public void Update()
34 {
35 FindRemoteAnimator();
36
37 m_SlideIn = Mathf.Lerp( m_SlideIn, m_IsOpen ? 1f : 0f, Time.deltaTime * 9f );
38 m_FoundPlayerSlideIn = Mathf.Lerp( m_FoundPlayerSlideIn, m_AnimatorView == null ? 0f : 1f, Time.deltaTime * 5f );
39 }
File name: DemoMecanimGUI.cs
Copy
42 public void FindRemoteAnimator()
43 {
44 if( m_RemoteAnimator != null )
45 {
46 return;
47 }
48
49 // the prefab has to be tagged as Player
50 GameObject[] gos = GameObject.FindGameObjectsWithTag( "Player" );
51 for( int i = 0; i < gos.Length; ++i )
52 {
53 PhotonView view = gos[ i ].GetComponent
54 if( view != null && view.isMine == false )
55 {
56 m_RemoteAnimator = gos[ i ].GetComponent
57 }
58 }
59 }
File name: DemoMecanimGUI.cs
Copy
61 public void OnGUI()
62 {
63 GUI.skin = Skin;
64
65 string[] synchronizeTypeContent = new string[] { "Disabled", "Discrete", "Continuous" };
66
67 GUILayout.BeginArea( new Rect( Screen.width - 200 * m_FoundPlayerSlideIn - 400 * m_SlideIn, 0, 600, Screen.height ), GUI.skin.box );
68 {
69 GUILayout.Label( "Mecanim Demo", GUI.skin.customStyles[ 0 ] );
70
71 GUI.color = Color.white;
72 string label = "Settings";
73
74 if( m_IsOpen == true )
75 {
76 label = "Close";
77 }
78
79 if( GUILayout.Button( label, GUILayout.Width( 110 ) ) )
80 {
81 m_IsOpen = !m_IsOpen;
82 }
83
84 string parameters = "";
85
86 if( m_AnimatorView != null )
87 {
88 parameters += "Send Values:\n";
89
90 for( int i = 0; i < m_AnimatorView.GetSynchronizedParameters().Count; ++i )
91 {
92 PhotonAnimatorView.SynchronizedParameter parameter = m_AnimatorView.GetSynchronizedParameters()[ i ];
93
94 try
95 {
96 switch( parameter.Type )
97 {
98 case PhotonAnimatorView.ParameterType.Bool:
99 parameters += parameter.Name + " (" + ( m_AnimatorView.GetComponent
100 break;
101 case PhotonAnimatorView.ParameterType.Int:
102 parameters += parameter.Name + " (" + m_AnimatorView.GetComponent
103 break;
104 case PhotonAnimatorView.ParameterType.Float:
105 parameters += parameter.Name + " (" + m_AnimatorView.GetComponent
106 break;
107 }
108 }
109 catch
110 {
111 Debug.Log( "derrrr for " + parameter.Name );
112 }
113 }
114 }
115
116 if( m_RemoteAnimator != null )
117 {
118 parameters += "\nReceived Values:\n";
119
120 for( int i = 0; i < m_AnimatorView.GetSynchronizedParameters().Count; ++i )
121 {
122 PhotonAnimatorView.SynchronizedParameter parameter = m_AnimatorView.GetSynchronizedParameters()[ i ];
123
124 try
125 {
126 switch( parameter.Type )
127 {
128 case PhotonAnimatorView.ParameterType.Bool:
129 parameters += parameter.Name + " (" + ( m_RemoteAnimator.GetBool( parameter.Name ) ? "True" : "False" ) + ")\n";
130 break;
131 case PhotonAnimatorView.ParameterType.Int:
132 parameters += parameter.Name + " (" + m_RemoteAnimator.GetInteger( parameter.Name ) + ")\n";
133 break;
134 case PhotonAnimatorView.ParameterType.Float:
135 parameters += parameter.Name + " (" + m_RemoteAnimator.GetFloat( parameter.Name ).ToString( "0.00" ) + ")\n";
136 break;
137 }
138 }
139 catch
140 {
141 Debug.Log( "derrrr for " + parameter.Name );
142 }
143 }
144 }
145
146 GUIStyle style = new GUIStyle( GUI.skin.label );
147 style.alignment = TextAnchor.UpperLeft;
148
149 GUI.color = new Color( 1f, 1f, 1f, 1 - m_SlideIn );
150 GUI.Label( new Rect( 10, 100, 600, Screen.height ), parameters, style );
151
152 if( m_AnimatorView != null )
153 {
154 GUI.color = new Color( 1f, 1f, 1f, m_SlideIn );
155
156 GUILayout.Space( 20 );
157 GUILayout.Label( "Synchronize Parameters" );
158
159 for( int i = 0; i < m_AnimatorView.GetSynchronizedParameters().Count; ++i )
160 {
161 GUILayout.BeginHorizontal();
162 {
163 PhotonAnimatorView.SynchronizedParameter parameter = m_AnimatorView.GetSynchronizedParameters()[ i ];
164
165 GUILayout.Label( parameter.Name, GUILayout.Width( 100 ), GUILayout.Height( 36 ) );
166
167 int selectedValue = (int)parameter.SynchronizeType;
168 int newValue = GUILayout.Toolbar( selectedValue, synchronizeTypeContent );
169
170 if( newValue != selectedValue )
171 {
172 m_AnimatorView.SetParameterSynchronized( parameter.Name, parameter.Type, (PhotonAnimatorView.SynchronizeType)newValue );
173 }
174 }
175 GUILayout.EndHorizontal();
176 }
177 }
178 }
179 GUILayout.EndArea();
180 }
Download file with original file name:Anim
Anim 122 lượt xem
Gõ tìm kiếm nhanh...