https://github.com/instance-id/usefulsnippets
A collection of Unity / Unity3d code snippets that I found useful. Perhaps others will as well.
https://github.com/instance-id/usefulsnippets
Last synced: about 1 month ago
JSON representation
A collection of Unity / Unity3d code snippets that I found useful. Perhaps others will as well.
- Host: GitHub
- URL: https://github.com/instance-id/usefulsnippets
- Owner: instance-id
- Created: 2020-09-20T23:47:38.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2020-09-22T16:44:25.000Z (over 5 years ago)
- Last Synced: 2025-11-15T11:14:57.812Z (4 months ago)
- Size: 12.7 KB
- Stars: 5
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# UsefulSnippets
A collection of Unity / Unity3d code snippets that I found useful. Perhaps others will as well.
## Color
#### GetColor() Extension
Usage:
```cs
// -- Standard Color
Color color = "#555555".GetColor()
// -- StyleColor
StyleColor styleColor = new StyleColor("#555555".GetColor());
```
Color Extension Code
```cs
using UnityEngine;
namespace instance.id.ColorExtensions
{
public static class ColorExtensions
{
public static Color GetColor(this string color)
{
ColorUtility.TryParseHtmlString(color, out var outColor);
return outColor;
}
}
```
## UIElements
#### UIElements - Draw IMGUI Reorderable Lists (2020.2)
|  |
| --- |
SerializedProperty Extension to determine if property is a List/Array
```cs
public static class PropertyExtensions
{
public static bool IsReallyArray(this SerializedProperty property)
{
return property.isArray && property.propertyType != SerializedPropertyType.String;
}
}
```
Draw IMGUI Reorderable Lists Code
```cs
[CustomEditor(typeof(Object), true, isFallback = true)]
[CanEditMultipleObjects]
public class DefaultEditorDrawer : Editor
{
public bool showScript;
public List excludedFields = new List();
string m_IMGUIPropNeedsRelayout;
ScrollView m_ScrollView;
public override VisualElement CreateInspectorGUI()
{
var root = new VisualElement();
var property = serializedObject.GetIterator();
m_ScrollView = new ScrollView();
root.Add(m_ScrollView);
if (property.NextVisible(true)) // Expand first child.
{
do
{
var field = new PropertyField(property) {name = "PropertyField:" + property.propertyPath};
if (property.propertyPath == "m_Script" && serializedObject.targetObject != null)
{
if (showScript) field.SetEnabled(false);
else continue;
}
if (property.IsReallyArray() && serializedObject.targetObject != null)
{
var copiedProperty = property.Copy();
var imDefaultProperty = new IMGUIContainer(() =>
{
DoDrawDefaultIMGUIProperty(serializedObject, copiedProperty);
}) {name = property.propertyPath};
m_ScrollView.Add(imDefaultProperty);
continue;
}
if (excludedFields.Contains(property.propertyPath) && serializedObject.targetObject != null) { continue; }
root.Add(field);
} while (property.NextVisible(false));
}
foreach (var foldout in m_ScrollView.Query().ToList())
{
foldout.RegisterValueChangedCallback(e =>
{
var fd = e.target as Foldout;
if (fd == null) return;
var path = fd.bindingPath;
var container = m_ScrollView.Q(name: path);
RecomputeSize(container);
});
}
return root;
}
public void RecomputeSize(IMGUIContainer container)
{
if (container == null) return;
var parent = container.parent;
container.RemoveFromHierarchy();
parent.Add(container);
}
public void DoDrawDefaultIMGUIProperty(SerializedObject serializedObject, SerializedProperty property)
{
EditorGUI.BeginChangeCheck();
serializedObject.Update();
bool wasExpanded = property.isExpanded;
EditorGUILayout.PropertyField(property, true);
if (property.isExpanded != wasExpanded) m_IMGUIPropNeedsRelayout = property.propertyPath;
serializedObject.ApplyModifiedProperties();
EditorGUI.EndChangeCheck();
}
}
```
---
