Builder
How do I use Builder
Below are practical examples compiled from projects for learning and reference purposes
Featured Snippets
File name: SupportLogger.cs
Copy
47 private void LogBasics()
48 {
49 StringBuilder sb = new StringBuilder();
50 sb.AppendFormat("SupportLogger Info: PUN {0}: ", PhotonNetwork.versionPUN);
51
52 sb.AppendFormat("AppID: {0}*** GameVersion: {1} ", PhotonNetwork.networkingPeer.mAppId.Substring(0, 8), PhotonNetwork.networkingPeer.mAppVersionPun);
53 sb.AppendFormat("Server: {0}. Region: {1} ", PhotonNetwork.ServerAddress, PhotonNetwork.networkingPeer.CloudRegion);
54 sb.AppendFormat("HostType: {0} ", PhotonNetwork.PhotonServerSettings.HostType);
55
56
57 Debug.Log(sb.ToString());
58 }
File name: AnimationBuilder.cs
Copy
112 private void MakeAnimationCurves(GameObject root, AnimationClip animClip, Animation animation)
113 {
114 acb = new AnimationCurveBuilder();
115
116 //Get a list of all sprites on this GO
117 var allSprites = root.GetComponentsInChildren
118
119 //Add a key for all objects on the first frame
120 SetGameObjectForKey(root, animClip, animation.MainlineKeys.First(), 0);
121
122 foreach (var mainlineKey in animation.MainlineKeys)
123 {
124
125 var visibleSprites = SetGameObjectForKey(root, animClip, mainlineKey);
126 var hiddenSprites = allSprites.Except(visibleSprites);
127 HideSprites(root, hiddenSprites, mainlineKey.Time);
128 }
129
130 switch (animation.LoopType)
131 {
132 case LoopType.True:
133 //Cycle back to first frame
134 SetGameObjectForKey(root, animClip, animation.MainlineKeys.First(), animation.Length);
135 break;
136 case LoopType.False:
137 //Duplicate the last key at the end time of the animation
138 SetGameObjectForKey(root, animClip, animation.MainlineKeys.Last(), animation.Length);
139 break;
140 default:
141 Debug.LogWarning("Unsupported loop type: " + animation.LoopType.ToString());
142 break;
143 }
144
145 //Add the curves to our animation clip
146 //NOTE: This MUST be done before modifying the settings, thus the double switch statement
147 acb.AddCurves(animClip);
148
149 foreach (var sprite in spriteChangeKeys)
150 {
151 if (sprite.Value.Count > 0)
152 {
153 BuildSpriteChangeCurve(ref animClip, sprite);
154 }
155 }
156
157 //Set the loop/wrap settings for the animation clip
158 var animSettings = AnimationUtility.GetAnimationClipSettings(animClip);
159 switch(animation.LoopType)
160 {
161 case LoopType.True:
162 animClip.wrapMode = WrapMode.Loop;
163 animSettings.loopTime = true;
164 break;
165 case LoopType.False:
166 animClip.wrapMode = WrapMode.ClampForever;
167 break;
168 case LoopType.PingPong:
169 animClip.wrapMode = WrapMode.PingPong;
170 animSettings.loopTime = true;
171 break;
172 default:
173 Debug.LogWarning("Unsupported loop type: " + animation.LoopType.ToString());
174 break;
175 }
176
177 animClip.SetAnimationSettings(animSettings);
178
179 //Debug.Log(string.Format("Setting animation {0} to {1} loop mode (WrapMode:{2} LoopTime:{3}) ", animClip.name, animation.LoopType, animClip.wrapMode, animSettings.loopTime));
180 }
File name: ScmlPostProcessor.cs
Copy
66 static void ImportScml(string assetPath)
67 {
68 string folderPath = Path.GetDirectoryName(assetPath);
69
70 //Load the SCML as XML
71 var doc = new XmlDocument();
72 doc.Load(assetPath);
73
74 //Parse the SCML file
75 var scml = new Spriter.ScmlObject(doc);
76
77 //TODO: Verify that all files/folders exist
78 var pb = new PrefabBuilder();
79 foreach (var entity in scml.Entities)
80 {
81 //TODO: Settings file to customize prefab location
82 var prefabPath = Path.Combine(folderPath, entity.Name + ".prefab");
83
84 //Change to forward slash for asset database friendliness
85 prefabPath = prefabPath.Replace('\\', '/');
86
87 //Either instantiate the existing prefab or create a new one
88 GameObject go;
89 var prefabGo = AssetDatabase.LoadAssetAtPath(prefabPath, typeof(GameObject));
90 if (prefabGo == null)
91 {
92 go = new GameObject();
93 prefabGo = PrefabUtility.CreatePrefab(prefabPath, go, ReplacePrefabOptions.ConnectToPrefab);
94 }
95 else
96 {
97 go = GameObject.Instantiate(prefabGo) as GameObject;
98
99 var oldAnimator = go.GetComponent
100 if (oldAnimator) GameObject.DestroyImmediate(oldAnimator);
101 }
102
103 //Build the prefab based on the supplied entity
104 pb.MakePrefab(entity, go, folderPath);
105
106 var animator = go.AddComponent
107
108
109
110 //Add animations to prefab object
111 var anim = new AnimationBuilder();
112 var allAnimClips = anim.BuildAnimationClips(go, entity, prefabPath);
113 AssetDatabase.SaveAssets();
114
115 var animatorControllerPath = Path.ChangeExtension(prefabPath, "controller");
116 UnityEditor.Animations.AnimatorController oldController = (UnityEditor.Animations.AnimatorController)AssetDatabase.LoadAssetAtPath(animatorControllerPath, typeof (UnityEditor.Animations.AnimatorController));
117 UnityEditor.Animations.AnimatorController controller = oldController;
118
119 if (!oldController)
120 {
121 controller = UnityEditor.Animations.AnimatorController.CreateAnimatorControllerAtPath(animatorControllerPath);
122 foreach (var animationClip in allAnimClips)
123 {
124 if (animationClip)
125 {
126 //UnityEditor.Animations.AnimatorController.AddAnimationClipToController(controller, animationClip);
127 }
128 }
129 }
130 UnityEditor.Animations.AnimatorController.SetAnimatorController(animator, controller);
131 go.SetActive(true);
132 //Update the prefab
133 PrefabUtility.ReplacePrefab(go, prefabGo, ReplacePrefabOptions.ConnectToPrefab);
134
135 //Add a generic avatar - because why not?
136 //TODO: May need to eventually break this into a separate class
137 // ie: if we want to look for a root motion node by naming convention
138 //var avatar = AvatarBuilder.BuildGenericAvatar(go, "");
139 //avatar.name = go.name;
140 //AssetDatabase.AddObjectToAsset(avatar, prefabPath);
141
142 GameObject.DestroyImmediate(go);
143
144 AssetDatabase.SaveAssets();
145 }
146 }
File name: ImportTiled2Unity.Material.cs
Copy
19 public Material FixMaterialForMeshRenderer(string objName, Renderer renderer)
20 {
21 string xmlPath = ImportUtils.GetXmlPath(objName);
22
23 XDocument xml = XDocument.Load(xmlPath);
24
25 // The mesh to match
26 string meshName = renderer.name;
27
28 // The mesh name may be decorated by Unity
29 string pattern = @"_MeshPart[\d]$";
30 Regex regex = new Regex(pattern);
31 meshName = regex.Replace(meshName, "");
32
33 var assignMaterials = xml.Root.Elements("AssignMaterial");
34
35 // Find an assignment that matches the mesh renderer
36 XElement match = assignMaterials.FirstOrDefault(el => el.Attribute("mesh").Value == meshName);
37
38 if (match == null)
39 {
40 // The names of our meshes in the AssignMaterials elements may be wrong
41 // This happened before when Unity replaced whitespace with underscore in our named meshes
42 // That case is handled now, but there may be others
43 StringBuilder builder = new StringBuilder();
44 builder.AppendFormat("Could not find mesh named '{0}' for material matching\n", renderer.name);
45 string choices = String.Join("\n ", assignMaterials.Select(m => m.Attribute("mesh").Value).ToArray());
46 builder.AppendFormat("Choices are:\n {0}", choices);
47
48 Debug.LogError(builder.ToString());
49 return null;
50 }
51
52 string materialName = match.Attribute("material").Value;
53 string materialPath = ImportUtils.GetMaterialPath(materialName);
54
55 // Assign the material
56 renderer.sharedMaterial = AssetDatabase.LoadAssetAtPath(materialPath, typeof(Material)) as Material;
57
58 // Set the sorting layer for the mesh
59 string sortingLayer = match.Attribute("sortingLayerName").Value;
60 if (!String.IsNullOrEmpty(sortingLayer) && !SortingLayerExposedEditor.GetSortingLayerNames().Contains(sortingLayer))
61 {
62 Debug.LogError(string.Format("Sorting Layer \"{0}\" does not exist. Check your Project Settings -> Tags and Layers", sortingLayer));
63 renderer.sortingLayerName = "Default";
64 }
65 else
66 {
67 renderer.sortingLayerName = sortingLayer;
68 }
69
70 // Set the sorting order
71 renderer.sortingOrder = ImportUtils.GetAttributeAsInt(match, "sortingOrder");
72
73 // Do we have an alpha color key?
74 string htmlColor = ImportUtils.GetAttributeAsString(match, "alphaColorKey", "");
75 if (!String.IsNullOrEmpty(htmlColor))
76 {
77 // Take for granted color is in the form '#RRGGBB'
78 byte r = byte.Parse(htmlColor.Substring(1, 2), System.Globalization.NumberStyles.HexNumber);
79 byte g = byte.Parse(htmlColor.Substring(3, 2), System.Globalization.NumberStyles.HexNumber);
80 byte b = byte.Parse(htmlColor.Substring(5, 2), System.Globalization.NumberStyles.HexNumber);
81 Color color = new Color32(r, g, b, 255);
82 renderer.sharedMaterial.SetColor("_AlphaColorKey", color);
83 }
84
85 return renderer.sharedMaterial;
86 }
File name: frmCustomers.cs
Copy
47 public static string GetUniqueKey(int maxSize)
48 {
49 char[] chars = new char[62];
50 chars = "123456789".ToCharArray();
51 byte[] data = new byte[1];
52 RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider();
53 crypto.GetNonZeroBytes(data);
54 data = new byte[maxSize];
55 crypto.GetNonZeroBytes(data);
56 StringBuilder result = new StringBuilder(maxSize);
57 foreach (byte b in data)
58 {
59 result.Append(chars[b % (chars.Length)]);
60 }
61 return result.ToString();
62 }
File name: frmOrder.cs
Copy
31 public static string GetUniqueKey(int maxSize)
32 {
33 char[] chars = new char[62];
34 chars = "123456789".ToCharArray();
35 byte[] data = new byte[1];
36 RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider();
37 crypto.GetNonZeroBytes(data);
38 data = new byte[maxSize];
39 crypto.GetNonZeroBytes(data);
40 StringBuilder result = new StringBuilder(maxSize);
41 foreach (byte b in data)
42 {
43 result.Append(chars[b % (chars.Length)]);
44 }
45 return result.ToString();
46 }
File name: frmPlaceOrders.cs
Copy
31 public static string GetUniqueKey(int maxSize)
32 {
33 char[] chars = new char[62];
34 chars = "123456789".ToCharArray();
35 byte[] data = new byte[1];
36 RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider();
37 crypto.GetNonZeroBytes(data);
38 data = new byte[maxSize];
39 crypto.GetNonZeroBytes(data);
40 StringBuilder result = new StringBuilder(maxSize);
41 foreach (byte b in data)
42 {
43 result.Append(chars[b % (chars.Length)]);
44 }
45 return result.ToString();
46 }
File name: frmProduct.cs
Copy
30 public static string GetUniqueKey(int maxSize)
31 {
32 char[] chars = new char[62];
33 chars = "123456789".ToCharArray();
34 byte[] data = new byte[1];
35 RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider();
36 crypto.GetNonZeroBytes(data);
37 data = new byte[maxSize];
38 crypto.GetNonZeroBytes(data);
39 StringBuilder result = new StringBuilder(maxSize);
40 foreach (byte b in data)
41 {
42 result.Append(chars[b % (chars.Length)]);
43 }
44 return result.ToString();
45 }
File name: frmSales.cs
Copy
31 public static string GetUniqueKey(int maxSize)
32 {
33 char[] chars = new char[62];
34 chars = "123456789".ToCharArray();
35 byte[] data = new byte[1];
36 RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider();
37 crypto.GetNonZeroBytes(data);
38 data = new byte[maxSize];
39 crypto.GetNonZeroBytes(data);
40 StringBuilder result = new StringBuilder(maxSize);
41 foreach (byte b in data)
42 {
43 result.Append(chars[b % (chars.Length)]);
44 }
45 return result.ToString();
46 }
File name: frmStock.cs
Copy
51 public static string GetUniqueKey(int maxSize)
52 {
53 char[] chars = new char[62];
54 chars =
55 "123456789".ToCharArray();
56 byte[] data = new byte[1];
57 RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider();
58 crypto.GetNonZeroBytes(data);
59 data = new byte[maxSize];
60 crypto.GetNonZeroBytes(data);
61 StringBuilder result = new StringBuilder(maxSize);
62 foreach (byte b in data)
63 {
64 result.Append(chars[b % (chars.Length)]);
65 }
66 return result.ToString();
67 }
Download file with original file name:Builder
Builder 92 lượt xem
Gõ tìm kiếm nhanh...