/*WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW*\ ( ( ) )
|/ \| ) ) _((_
|| (c) Wanzyee Studio < wanzyeestudio.blogspot.com > || ( ( |_ _ |=n
|\ /| _____)) | ! ] U
\.ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ./ (_(__(S) |___*/
using UnityEditor;
using UnityEngine;
using System;
using System.Linq;
using Object = UnityEngine.Object;
namespace WanzyeeStudio.Editrix.Toolkit{
///
/// Set UnityEngine.Object to be locked or editable.
///
///
///
/// Set UnityEngine.HideFlags switch bit of HideFlags.NotEditable to avoid missing operation.
/// Use context menu "Toggle Locked", or menu "Edit/Toggle Locked" with hotkey ALT-Shift-L for selections.
///
///
public static class ObjectLocker{
#region Menu
///
/// Toggle the object locked or editable.
///
/// Command.
[MenuItem("CONTEXT/Object/Toggle Locked", false, 10010)]
private static void ObjectToggleLocked(MenuCommand command){
SetLocked(!GetLocked(command.context), command.context);
}
///
/// Toggle the selected objects locked or editable to the opposite of the first one, with hotkey Alt-Shift-L.
///
[MenuItem("Edit/Toggle Locked l", false, 200)]
public static void EditToggleLocked(){
if(!EditToggleLockedValid()) throw new InvalidOperationException("Nothing is selected.");
SetLocked(!GetLocked(Selection.objects.First(_v => null != _v)), Selection.objects);
}
///
/// Check if EditToggleLocked() valid, any UnityEngine.Object selected.
///
/// true, if valid.
[MenuItem("Edit/Toggle Locked l", true)]
private static bool EditToggleLockedValid(){
return (null != Selection.activeObject);
}
#endregion
#region Methods
///
/// Determine if the object locked or editable.
///
/// true, if locked, false otherwise.
/// Target.
public static bool GetLocked(Object target){
if(null == target) throw new ArgumentNullException("obj");
return HideFlags.NotEditable == (target.hideFlags & HideFlags.NotEditable);
}
///
/// Set the objects locked or editable.
///
///
///
/// Only switch HideFlags.NotEditable, and keep other hideFlags bit.
/// Note, to set GameObjcet.hideFlags will also set all components on it.
///
///
/// If set to true locked.
/// Targets.
///
/*
* Use Undo.RegisterCompleteObjectUndo, since Undo.RecordObject doesn't work for HideFlags.
* Use Undo.RegisterFullObjectHierarchyUndo, since the above one doesn't work for Component on GameObject.
*/
public static void SetLocked(bool locked, params Object[] targets){
if(null == targets) throw new ArgumentNullException("objects");
foreach(var _v in targets.Where(_o => null != _o).Distinct()){
if(_v is GameObject) Undo.RegisterFullObjectHierarchyUndo(_v, "Set Locked");
else Undo.RegisterCompleteObjectUndo(_v, "Set Locked");
if(locked) _v.hideFlags |= HideFlags.NotEditable;
else _v.hideFlags &= ~HideFlags.NotEditable;
EditorUtility.SetDirty(_v);
}
}
#endregion
}
}