Skip to content

Instantly share code, notes, and snippets.

@anatawa12
Created February 12, 2023 08:34
Show Gist options
  • Select an option

  • Save anatawa12/58997264029d15b44d1bfcd0df927b1d to your computer and use it in GitHub Desktop.

Select an option

Save anatawa12/58997264029d15b44d1bfcd0df927b1d to your computer and use it in GitHub Desktop.
A trial to move component without clone. This does not work.
/*
* Move Component Test
* https://gist.github.com/anatawa12/b8799da5d3131e4020f414439a4ea037
*
* A trial to move component without clone. This does not work well
*
* MIT License
*
* Copyright (c) 2023 anatawa12
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#if UNITY_EDITOR
using UnityEditor;
using UnityEngine;
namespace anatawa12.gists
{
public class MoveComponentTest : EditorWindow
{
public Component component;
public GameObject target;
private SerializedObject _serialized;
private SerializedProperty _component;
private SerializedProperty _target;
[MenuItem("Tools/anatawa12 gists/Move Compoent Test")]
private static void Open() => CreateWindow<MoveComponentTest>();
private void OnEnable()
{
_serialized = new SerializedObject(this);
_component = _serialized.FindProperty(nameof(component));
_target = _serialized.FindProperty(nameof(target));
}
private void OnGUI()
{
_serialized.Update();
EditorGUILayout.ObjectField(_component);
EditorGUILayout.ObjectField(_target);
_serialized.ApplyModifiedProperties();
using (new EditorGUI.DisabledScope(!component || !target || component.gameObject == target))
{
if (GUILayout.Button("Move Component"))
{
var componentSerialized = new SerializedObject(component);
var oldGameObjectSerialized = new SerializedObject(component.gameObject);
var newGameObjectSerialized = new SerializedObject(target);
var gameObjectProp = componentSerialized.FindProperty("m_GameObject");
var oldComponentsProp = oldGameObjectSerialized.FindProperty("m_Component");
for (var i = 0; i < oldComponentsProp.arraySize; i++)
{
if (oldComponentsProp.GetArrayElementAtIndex(i).FindPropertyRelative("component")
.objectReferenceValue == component)
{
oldComponentsProp.DeleteArrayElementAtIndex(i);
break;
}
}
var newComponentsProp = newGameObjectSerialized.FindProperty("m_Component");
newComponentsProp.arraySize += 1;
newComponentsProp.GetArrayElementAtIndex(newComponentsProp.arraySize - 1)
.FindPropertyRelative("component").objectReferenceValue = component;
gameObjectProp.objectReferenceValue = target;
oldGameObjectSerialized.ApplyModifiedProperties();
componentSerialized.ApplyModifiedProperties();
newGameObjectSerialized.ApplyModifiedProperties();
}
}
}
}
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment