Skip to content

Instantly share code, notes, and snippets.

@giobel
Last active December 15, 2020 03:16
Show Gist options
  • Save giobel/429c63d816e0a872e1b4d9439608aa19 to your computer and use it in GitHub Desktop.
Save giobel/429c63d816e0a872e1b4d9439608aa19 to your computer and use it in GitHub Desktop.
Unity
*Unity C#*
// BatchRename.cs
// Unity Editor extension that allows batch renaming for GameObjects in Hierarchy
// Via Alan Thorn (TW: @thorn_alan)
// Roman Luks - added sorting by sibling index
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
using System.Linq;
public class Renamer : ScriptableWizard
{
/// <summary>
/// Base name
/// </summary>
public string BaseName = "MyObject_";
/// <summary>
/// Start count
/// </summary>
public int StartNumber = 0;
/// <summary>
/// Increment
/// </summary>
public int Increment = 1;
public string Search = "";
public string Replace = "";
[MenuItem("Edit/Batch Rename...")]
static void CreateWizard()
{
ScriptableWizard.DisplayWizard("Batch Rename", typeof(Renamer), "Rename");
}
/// <summary>
/// Called when the window first appears
/// </summary>
void OnEnable()
{
UpdateSelectionHelper();
}
/// <summary>
/// Function called when selection changes in scene
/// </summary>
void OnSelectionChange()
{
UpdateSelectionHelper();
}
/// <summary>
/// Update selection counter
/// </summary>
void UpdateSelectionHelper()
{
helpString = "";
if (Selection.objects != null)
helpString = "Number of objects selected: " + Selection.objects.Length;
}
/// <summary>
/// Rename
/// </summary>
void OnWizardCreate()
{
// If selection is empty, then exit
if (Selection.objects == null)
return;
// Current Increment
int PostFix = StartNumber;
List<GameObject> mySelection = new List<GameObject>(Selection.gameObjects);
mySelection.Sort((go1, go2) => go1.transform.GetSiblingIndex().CompareTo(go2.transform.GetSiblingIndex()));
foreach (var item in mySelection)
{
//item.name = BaseName + PostFix;
//PostFix += Increment;
item.name = item.name.Replace(Search, Replace);
}
}
}
public static List<string> GetRevitParametersFromObject(Transform gameObjectsTransform, SpeckleUnityManager speckleManager)
{
List<string> revitParameters = new List<string>();
GameObject childrenObject = gameObjectsTransform.gameObject;
//Debug.Log($"{childrenObject}: {manager.TryGetSpeckleObject(childrenObject, out SpeckleObject data)}");
if (speckleManager.TryGetSpeckleObject(childrenObject, out SpeckleObject data))
{
//get the required property
if (data.Properties.TryGetValue("parameters", out object propertyValue))
{
Dictionary<string, object> temp = propertyValue as Dictionary<string, object>;
foreach (var item in temp.Keys)
{
if (!item.StartsWith("_"))
revitParameters.Add(item);
}
}
}
return revitParameters;
}
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//https://stackoverflow.com/questions/36239705/serialize-and-deserialize-json-and-json-array-in-unity
public static class JsonHelper
{
public static T[] FromJson<T>(string json)
{
Wrapper<T> wrapper = JsonUtility.FromJson<Wrapper<T>>(json);
return wrapper.Items;
}
public static string ToJson<T>(T[] array)
{
Wrapper<T> wrapper = new Wrapper<T>();
wrapper.Items = array;
return JsonUtility.ToJson(wrapper);
}
public static string ToJson<T>(T[] array, bool prettyPrint)
{
Wrapper<T> wrapper = new Wrapper<T>();
wrapper.Items = array;
return JsonUtility.ToJson(wrapper, prettyPrint);
}
[Serializable]
private class Wrapper<T>
{
public T[] Items;
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
public class jsonReader : MonoBehaviour
{
string fixJson(string value)
{
value = "{\"Items\":" + value + "}";
return value;
}
// Start is called before the first frame update
[System.Diagnostics.CodeAnalysis.SuppressMessage("Compiler", "IDE0043:Invalid format string", Justification = "<Pending>")]
void Start()
{
string jsonFile = @"Assets/temp.json";
string json = File.ReadAllText(jsonFile);
//TextAsset spellData = Resources.Load("temp.json") as TextAsset;
//string jsonString = fixJson(json);
//string jsonString = "{\"Items\": [ { \"ElementId\": \"8484239823\", \"Comments\": \"Powai\", \"playerNick\": \"Random Nick\" }, { \"ElementId\": \"512343283\", \"Comments\": \"User2\", \"playerNick\": \"Rand Nick 2\" } ]}";
// string jsonString = "{\"Items\":[ {\"ElementId\":12345,\"Width\":125.2,\"Height\":300.5,\"Comments\":\"My Comments\"}, {\"ElementId\":12346,\"Width\":125.2,\"Height\":300.5,\"Comments\":\"My Comments\"}, {\"ElementId\":12347,\"Width\":125.2,\"Height\":300.5,\"Comments\":\"My Comments\"}, {\"ElementId\":12348,\"Width\":125.2,\"Height\":300.5,\"Comments\":\"My Comments\"}, {\"ElementId\":12349,\"Width\":125.2,\"Height\":300.5,\"Comments\":\"My Comments\"}, {\"ElementId\":12350,\"Width\":125.2,\"Height\":300.5,\"Comments\":\"My Comments\"}, {\"ElementId\":12351,\"Width\":125.2,\"Height\":300.5,\"Comments\":\"My Comments\"}, {\"ElementId\":12352,\"Width\":125.2,\"Height\":300.5,\"Comments\":\"My Comments\"}, {\"ElementId\":12353,\"Width\":125.2,\"Height\":300.5,\"Comments\":\"My Comments\"}, {\"ElementId\":12354,\"Width\":125.2,\"Height\":300.5,\"Comments\":\"My Comments \"}] }";
//Debug.Log(jsonString);
PanelData[] objects = JsonHelper.FromJson<PanelData>(json);
Debug.Log("Done");
Debug.Log(json);
Debug.Log(objects.Length);
Debug.Log($"{objects[9].ElementId}, " +
$"{objects[9].Width}, {objects[9].Height}, {objects[9].Comments} ");
}
}
{"Items":[
{"ElementId":"12345","Width":"125.2","Height":"300.5","Comments":"My Comments"},
{"ElementId":"12346","Width":"125.2","Height":"300.5","Comments":"My Comments"},
{"ElementId":"12347","Width":"125.2","Height":"300.5","Comments":"My Comments"},
{"ElementId":"12348","Width":"125.2","Height":"300.5","Comments":"My Comments"},
{"ElementId":"12349","Width":"125.2","Height":"300.5","Comments":"My Comments"},
{"ElementId":"12350","Width":"125.2","Height":"300.5","Comments":"My Comments"},
{"ElementId":"12351","Width":"125.2","Height":"300.5","Comments":"My Comments"},
{"ElementId":"12352","Width":"125.2","Height":"300.5","Comments":"My Comments"},
{"ElementId":"12353","Width":"125.2","Height":"300.5","Comments":"My Comments"},
{"ElementId":"12354","Width":"125.2","Height":"300.5","Comments":"My Comments 9"}]
}
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[Serializable]
public class PanelData
{
public string ElementId;
public string Width;
public string Height;
public string Comments;
}
string simpleJsonTest = "Assets/simpleJsonTest.json";
string json = File.ReadAllText(simpleJsonTest);
// Place the json file in the Resource folder
// TextAsset jsonTextFile = Resources.Load<TextAsset>("simpleJsonTest");
// string txt = jsonTextFile.text;
var items = JSON.Parse(json)["Items"];
for (int i = 0; i < items.Count; i++)
{
Debug.Log(items[i]["ElementId"]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment