Simply
How do I use Simply
Below are practical examples compiled from projects for learning and reference purposes
Featured Snippets
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: PhotonConverter.cs
Copy
189 static void ConvertToPhotonAPI(string file)
190 {
191 string text = File.ReadAllText(file);
192
193 bool isJS = file.Contains(".js");
194
195 file = file.Replace("\\", "/"); // Get Class name for JS
196 string className = file.Substring(file.LastIndexOf("/")+1);
197 className = className.Substring(0, className.IndexOf("."));
198
199
200 //REGEXP STUFF
201 //Valid are: Space { } , /n /r
202 //string NOT_VAR = @"([^A-Za-z0-9_\[\]\.]+)";
203 string NOT_VAR_WITH_DOT = @"([^A-Za-z0-9_]+)";
204
205 //string VAR_NONARRAY = @"[^A-Za-z0-9_]";
206
207
208 //NetworkView
209 {
210 text = PregReplace(text, NOT_VAR_WITH_DOT + "NetworkView" + NOT_VAR_WITH_DOT, "$1PhotonView$2");
211 text = PregReplace(text, NOT_VAR_WITH_DOT + "networkView" + NOT_VAR_WITH_DOT, "$1photonView$2");
212 text = PregReplace(text, NOT_VAR_WITH_DOT + "stateSynchronization" + NOT_VAR_WITH_DOT, "$1synchronization$2");
213 text = PregReplace(text, NOT_VAR_WITH_DOT + "NetworkStateSynchronization" + NOT_VAR_WITH_DOT, "$1ViewSynchronization$2"); // map Unity enum to ours
214 //.RPC
215 text = PregReplace(text, NOT_VAR_WITH_DOT + "RPCMode.Server" + NOT_VAR_WITH_DOT, "$1PhotonTargets.MasterClient$2");
216 text = PregReplace(text, NOT_VAR_WITH_DOT + "RPCMode" + NOT_VAR_WITH_DOT, "$1PhotonTargets$2");
217 }
218
219 //NetworkMessageInfo: 100%
220 {
221 text = PregReplace(text, NOT_VAR_WITH_DOT + "NetworkMessageInfo" + NOT_VAR_WITH_DOT, "$1PhotonMessageInfo$2");
222 text = PregReplace(text, NOT_VAR_WITH_DOT + "networkView" + NOT_VAR_WITH_DOT, "$1photonView$2");
223 }
224
225 //NetworkViewID:
226 {
227 text = PregReplace(text, NOT_VAR_WITH_DOT + "NetworkViewID" + NOT_VAR_WITH_DOT, "$1int$2"); //We simply use an int
228 }
229
230 //NetworkPlayer
231 {
232 text = PregReplace(text, NOT_VAR_WITH_DOT + "NetworkPlayer" + NOT_VAR_WITH_DOT, "$1PhotonPlayer$2");
233 }
234
235 //Network
236 {
237 //Monobehaviour callbacks
238 {
239 text = PregReplace(text, NOT_VAR_WITH_DOT + "OnPlayerConnected" + NOT_VAR_WITH_DOT, "$1OnPhotonPlayerConnected$2");
240 text = PregReplace(text, NOT_VAR_WITH_DOT + "OnPlayerDisconnected" + NOT_VAR_WITH_DOT, "$1OnPhotonPlayerDisconnected$2");
241 text = PregReplace(text, NOT_VAR_WITH_DOT + "OnNetworkInstantiate" + NOT_VAR_WITH_DOT, "$1OnPhotonInstantiate$2");
242 text = PregReplace(text, NOT_VAR_WITH_DOT + "OnSerializeNetworkView" + NOT_VAR_WITH_DOT, "$1OnPhotonSerializeView$2");
243 text = PregReplace(text, NOT_VAR_WITH_DOT + "BitStream" + NOT_VAR_WITH_DOT, "$1PhotonStream$2");
244
245 //Not completely the same meaning
246 text = PregReplace(text, NOT_VAR_WITH_DOT + "OnServerInitialized" + NOT_VAR_WITH_DOT, "$1OnCreatedRoom$2");
247 text = PregReplace(text, NOT_VAR_WITH_DOT + "OnConnectedToServer" + NOT_VAR_WITH_DOT, "$1OnJoinedRoom$2");
248
249 text = PregReplace(text, NOT_VAR_WITH_DOT + "OnFailedToConnectToMasterServer" + NOT_VAR_WITH_DOT, "$1OnFailedToConnectToPhoton$2");
250 text = PregReplace(text, NOT_VAR_WITH_DOT + "OnFailedToConnect" + NOT_VAR_WITH_DOT, "$1OnFailedToConnect_OBSELETE$2");
251 }
252
253 //Variables
254 {
255
256 text = PregReplace(text, NOT_VAR_WITH_DOT + "Network.connections" + NOT_VAR_WITH_DOT, "$1PhotonNetwork.playerList$2");
257 text = PregReplace(text, NOT_VAR_WITH_DOT + "Network.isServer" + NOT_VAR_WITH_DOT, "$1PhotonNetwork.isMasterClient$2");
258 text = PregReplace(text, NOT_VAR_WITH_DOT + "Network.isClient" + NOT_VAR_WITH_DOT, "$1PhotonNetwork.isNonMasterClientInRoom$2");
259
260 text = PregReplace(text, NOT_VAR_WITH_DOT + "NetworkPeerType" + NOT_VAR_WITH_DOT, "$1ConnectionState$2");
261 text = PregReplace(text, NOT_VAR_WITH_DOT + "Network.peerType" + NOT_VAR_WITH_DOT, "$1PhotonNetwork.connectionState$2");
262 text = PregReplace(text, NOT_VAR_WITH_DOT + "ConnectionState.Server" + NOT_VAR_WITH_DOT, "$1PhotonNetwork.isMasterClient$2");
263 text = PregReplace(text, NOT_VAR_WITH_DOT + "ConnectionState.Client" + NOT_VAR_WITH_DOT, "$1PhotonNetwork.isNonMasterClientInRoom$2");
264 text = PregReplace(text, NOT_VAR_WITH_DOT + "PhotonNetwork.playerList.Length" + NOT_VAR_WITH_DOT, "$1PhotonNetwork.playerList.Count$2");
265
266 /*DROPPED:
267 minimumAllocatableViewIDs
268 natFacilitatorIP is dropped
269 natFacilitatorPort is dropped
270 connectionTesterIP
271 connectionTesterPort
272 proxyIP
273 proxyPort
274 useProxy
275 proxyPassword
276 */
277 }
278
279 //Methods
280 {
281 text = PregReplace(text, NOT_VAR_WITH_DOT + "Network.InitializeServer" + NOT_VAR_WITH_DOT, "$1PhotonNetwork.CreateRoom$2");
282 text = PregReplace(text, NOT_VAR_WITH_DOT + "Network.Connect" + NOT_VAR_WITH_DOT, "$1PhotonNetwork.JoinRoom$2");
283 text = PregReplace(text, NOT_VAR_WITH_DOT + "Network.GetAveragePing" + NOT_VAR_WITH_DOT, "$1PhotonNetwork.GetPing$2");
284 text = PregReplace(text, NOT_VAR_WITH_DOT + "Network.GetLastPing" + NOT_VAR_WITH_DOT, "$1PhotonNetwork.GetPing$2");
285 /*DROPPED:
286 TestConnection
287 TestConnectionNAT
288 HavePublicAddress
289 */
290 }
291
292 //Overall
293 text = PregReplace(text, NOT_VAR_WITH_DOT + "Network" + NOT_VAR_WITH_DOT, "$1PhotonNetwork$2");
294
295
296 //Changed methods
297 string ignoreMe = @"([A-Za-z0-9_\[\]\(\) ]+)";
298
299 text = PregReplace(text, NOT_VAR_WITH_DOT + "PhotonNetwork.GetPing\\(" + ignoreMe+"\\);", "$1PhotonNetwork.GetPing();");
300 text = PregReplace(text, NOT_VAR_WITH_DOT + "PhotonNetwork.CloseConnection\\(" + ignoreMe+","+ignoreMe+"\\);", "$1PhotonNetwork.CloseConnection($2);");
301
302 }
303
304 //General
305 {
306 if (text.Contains("Photon")) //Only use the PhotonMonoBehaviour if we use photonView and friends.
307 {
308 if (isJS)//JS
309 {
310 if (text.Contains("extends MonoBehaviour"))
311 text = PregReplace(text, "extends MonoBehaviour", "extends Photon.MonoBehaviour");
312 else
313 text = "class " + className + " extends Photon.MonoBehaviour {\n" + text + "\n}";
314 }
315 else //C#
316 text = PregReplace(text, ": MonoBehaviour", ": Photon.MonoBehaviour");
317 }
318 }
319
320 File.WriteAllText(file, text);
321 }
File name: PhotonViewHandler.cs
Copy
25 internal static void HierarchyChange()
26 {
27 if (Application.isPlaying)
28 {
29 //Debug.Log("HierarchyChange ignored, while running.");
30 CheckSceneForStuckHandlers = true; // done once AFTER play mode.
31 return;
32 }
33
34 if (CheckSceneForStuckHandlers)
35 {
36 CheckSceneForStuckHandlers = false;
37 PhotonNetwork.InternalCleanPhotonMonoFromSceneIfStuck();
38 }
39
40 HashSet
41 HashSet
42 bool fixedSomeId = false;
43
44 //// the following code would be an option if we only checked scene objects (but we can check all PVs)
45 //PhotonView[] pvObjects = GameObject.FindSceneObjectsOfType(typeof(PhotonView)) as PhotonView[];
46 //Debug.Log("HierarchyChange. PV Count: " + pvObjects.Length);
47
48 string levelName = Application.loadedLevelName;
49 #if UNITY_EDITOR
50 levelName = System.IO.Path.GetFileNameWithoutExtension(EditorApplication.currentScene);
51 #endif
52 int minViewIdInThisScene = PunSceneSettings.MinViewIdForScene(levelName);
53 //Debug.Log("Level '" + Application.loadedLevelName + "' has a minimum ViewId of: " + minViewIdInThisScene);
54
55 PhotonView[] pvObjects = Resources.FindObjectsOfTypeAll(typeof(PhotonView)) as PhotonView[];
56
57 foreach (PhotonView view in pvObjects)
58 {
59 // first pass: fix prefabs to viewID 0 if they got a view number assigned (cause they should not have one!)
60 if (EditorUtility.IsPersistent(view.gameObject))
61 {
62 if (view.viewID != 0 || view.prefixBackup != -1 || view.instantiationId != -1)
63 {
64 Debug.LogWarning("PhotonView on persistent object being fixed (id and prefix must be 0). Was: " + view);
65 view.viewID = 0;
66 view.prefixBackup = -1;
67 view.instantiationId = -1;
68 EditorUtility.SetDirty(view);
69 fixedSomeId = true;
70 }
71 }
72 else
73 {
74 // keep all scene-instanced PVs for later re-check
75 pvInstances.Add(view);
76 }
77 }
78
79 Dictionary
80
81 // second pass: check all used-in-scene viewIDs for duplicate viewIDs (only checking anything non-prefab)
82 // scene-PVs must have user == 0 (scene/room) and a subId != 0
83 foreach (PhotonView view in pvInstances)
84 {
85 if (view.ownerId > 0)
86 {
87 Debug.Log("Re-Setting Owner ID of: " + view);
88 }
89 view.ownerId = 0; // simply make sure no owner is set (cause room always uses 0)
90 view.prefix = -1; // TODO: prefix could be settable via inspector per scene?!
91
92 if (view.viewID != 0)
93 {
94 if (view.viewID < minViewIdInThisScene || usedInstanceViewNumbers.Contains(view.viewID))
95 {
96 view.viewID = 0; // avoid duplicates and negative values by assigning 0 as (temporary) number to be fixed in next pass
97 }
98 else
99 {
100 usedInstanceViewNumbers.Add(view.viewID); // builds a list of currently used viewIDs
101
102 int instId = 0;
103 if (idPerObject.TryGetValue(view.gameObject, out instId))
104 {
105 view.instantiationId = instId;
106 }
107 else
108 {
109 view.instantiationId = view.viewID;
110 idPerObject[view.gameObject] = view.instantiationId;
111 }
112 }
113 }
114
115 }
116
117 // third pass: anything that's now 0 must get a new (not yet used) ID (starting at 0)
118 int lastUsedId = (minViewIdInThisScene > 0) ? minViewIdInThisScene - 1 : 0;
119
120 foreach (PhotonView view in pvInstances)
121 {
122 if (view.viewID == 0)
123 {
124 // Debug.LogWarning("setting scene ID: " + view.gameObject.name + " ID: " + view.subId.ID + " scene ID: " + view.GetSceneID() + " IsPersistent: " + EditorUtility.IsPersistent(view.gameObject) + " IsSceneViewIDFree: " + IsSceneViewIDFree(view.subId.ID, view));
125 int nextViewId = PhotonViewHandler.GetID(lastUsedId, usedInstanceViewNumbers);
126
127 view.viewID = nextViewId;
128
129 int instId = 0;
130 if (idPerObject.TryGetValue(view.gameObject, out instId))
131 {
132 Debug.Log("Set inst ID");
133 view.instantiationId = instId;
134 }
135 else
136 {
137 view.instantiationId = view.viewID;
138 idPerObject[view.gameObject] = nextViewId;
139 }
140
141 //// when using the Editor's serialization (view.subId in this case), this is not needed, it seems
142 //PrefabUtility.RecordPrefabInstancePropertyModifications(view);
143
144 lastUsedId = nextViewId;
145 EditorUtility.SetDirty(view);
146 fixedSomeId = true;
147 }
148 }
149
150
151 if (fixedSomeId)
152 {
153 //Debug.LogWarning("Some subId was adjusted."); // this log is only interesting for Exit Games
154 }
155 }
File name: InstructionsScript.cs
Copy
27 void OnGUI() {
28 if (this.gameScript.gameView == "instructions") {
29 GUI.skin = currentGUISkin;
30
31 this.gameScript.mainCamera.transform.eulerAngles = new Vector3 (120, 23, 0);
32
33 GUIStyle labelStyle = new GUIStyle(currentGUISkin.label);
34 labelStyle.alignment = TextAnchor.UpperLeft;
35 GUILayout.Label ("Instructions", "BigLabel");
36
37
38 scrollPosition = GUILayout.BeginScrollView(scrollPosition, GUILayout.Width(Screen.width), GUILayout.Height(Mathf.Ceil(Screen.height * .80f)));
39
40 GUILayout.Label ("Object", "Subheader");
41
42 GUILayout.Label (@"The object of 2048-3D is to"
43 + " slide numbered blocks in such a way"
44 + " so that blocks with the same numbers collide"
45 + " and combine into a new block that is twice"
46 + " as much as the originals until"
47 + " the number 2048 is reached.", labelStyle);
48
49 GUILayout.Label ("Moving the Blocks", "Subheader");
50
51 GUILayout.Label (@"You cannot move blocks individually, but must"
52 + " move all blocks simultaneously in the same direction."
53 + " Blocks can move forward, backward, up, down, left"
54 + " and right along the green connectors."
55 + " Simply swipe any part of the screen to move up,"
56 + " down, left or right (keyboard: arrow keys). To move the"
57 + " blocks forward and backward use the"
58 + " big red arrow keys at the bottom of the screen (keyboard: a and z keys)."
59 + " When moving, all blocks that can slide in the chosen direction will move."
60 + " Any block moving toward another block with the same number will collide "
61 + " and form a single block with twice the number as the originals", labelStyle);
62
63
64 GUILayout.Label ("New Blocks", "Subheader");
65
66 GUILayout.Label (@"After each move is"
67 + " made a new block will appear randomly in an empty position."
68 + " This block will have a number of either 2 or 4."
69 + " For an extra challenge, there is a game option you can"
70 + " set so that zeros can also be assinged to a new block."
71 + " Zeros act like any other number in that they can"
72 + " collide with other zeros to make a block twice as much "
73 + " (which is still zero).", labelStyle);
74
75
76
77 GUILayout.Label ("Scoring and Finishing", "Subheader");
78
79 GUILayout.Label(@"For every block collision that occurs you receive"
80 + " the number of points of the newly"
81 + " created block. If after making a move"
82 + " all positions are filled and no new"
83 + " moves are possible, the game ends."
84 + " A separate high score / highest block is kept for each"
85 + " distinct combination of game options", labelStyle);
86
87
88 GUILayout.Label ("Game Layout Options", "Subheader");
89
90 GUILayout.Label (@"When I first made this game there"
91 + " was only one game layout, a 3x3x3 cube."
92 + " After testing it a bit, it was way to easy"
93 + " so the zero option was added."
94 + " It was still way to easy "
95 + " (e.g. you could swipe without even looking and get pretty far)."
96 + " Therefore there are now several diffent game layouts that"
97 + " make the game more challenging and fun.", labelStyle);
98
99 GUILayout.Label ("Game Timer Option", "Subheader");
100
101 GUILayout.Label (@"To give yourself even more of a challenge"
102 + " you can set game options to include a timer."
103 + " If a timer is chosen you have a specific"
104 + " amount of time to combined blocks to make the 64 block."
105 + " If you run out of time the game is over."
106 + " If you reach your target before the timer runs down you will"
107 + " receive additional time to reach the next target."
108 + " The time you received is as follows: \n"
109 + " 64: option time + 5 seconds (because the first one is the hardest!)\n"
110 + " 128: option time\n"
111 + " 256: 2X option time\n"
112 + " 512: 4X option time \n"
113 + " 1024: 8X option time \n"
114 + " you get the idea.", labelStyle);
115
116
117 GUILayout.Label ("Acknowledgements \nand Confessions", "Subheader");
118
119 GUILayout.Label (@"2048-3D is based upon the original" +
120 " 2048 game designed by Gabriele Cirulli " +
121 " \n\n" +
122 " Sound effects by freeSFX http://www.freesfx.co.uk.\n\n" +
123 " This game was designed using the Unity3D game engine.\n\n" +
124 " FOR MORE PROJECTS VISIT:" +
125 " https://code-projects.org/", labelStyle);
126
127
128 foreach (Touch touch in Input.touches) {
129 if (touch.phase == TouchPhase.Moved)
130 {
131 // dragging
132 this.scrollPosition.y += touch.deltaPosition.y;
133 }
134 }
135 GUILayout.EndScrollView();
136
137 if (GUILayout.Button ("Return to Menu")) {
138 this.gameScript.gameView = "menu";
139 }
140 }
141 }
File name: PlayerCtrl.cs
Copy
102 void Jump()
103 {
104 if (isGrounded)
105 {
106 isJumping = true;
107 rb.AddForce(new Vector2(0, jumpSpeed)); // simply make the player jump in the y axis or upwards
108 anim.SetInteger("State", 2);
109
110 Invoke("EnableDoubleJump", delayForDoubleJump);
111 }
112
113 if (canDoubleJump && !isGrounded)
114 {
115 rb.velocity = Vector2.zero;
116 rb.AddForce(new Vector2(0, jumpSpeed)); // simply make the player jump in the y axis or upwards
117 anim.SetInteger("State", 2);
118
119 canDoubleJump = false;
120 }
121 }
Simply 141 lượt xem
Gõ tìm kiếm nhanh...