Unity 编辑器扩展(五)Selection

Selection

 Selection类是编辑器类,使用需要导入using UnityEditor,可以继承Editor,脚本最好放在Editor文件夹中。

属性:

public static UnityEngine.Object[] objects { get; set; }

 摘要:The actual unfiltered selection from the Scene.

返回一个数组,内容为当前点击的场景物体或Project资源(包括场景、脚本、预制等任意);不符合条件的当前选择不会加入到数组;未选择会返回长度为0的数组而不是null。

using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

public class Test
{
    [MenuItem(@"Selection/Test")]
    public static void MyTest()
    {
        Object[] objects = Selection.objects;

        foreach (var item in objects)
        {
            Debug.Log(item.name);
        }
    }
}

public static int activeInstanceID { get; set; } 

 摘要:Returns the instanceID of the actual object selection. Includes prefabs, non-modifyable objects.

返回实际选择的激活的物体的实例ID。包括预设物,不可改动的物体。

using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

public class Test
{
    [MenuItem(@"Selection/Test")]
    public static void MyTest()
    {
        int i = Selection.activeInstanceID;

        Debug.Log(i);
    }
}

public static UnityEngine.Object activeContext { get; } 

 摘要:Returns the current context object, as was set via SetActiveObjectWithContext.

返回通过SetActiveObjectWithContext设置的当前上下文对象。

public static UnityEngine.Object activeObject { get; set; }

摘要:Returns the actual object selection. Includes prefabs, non-modifyable objects.

返回当前点击的场景游戏物体或Project资源(包括场景、脚本、预制等任意);选择多个则返回第一个选择的;未选择相应的则返回null 。

using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

public class Test
{
    [MenuItem(@"Selection/Test")]
    public static void MyTest()
    {
        Object obj = Selection.activeObject;

        Debug.Log(obj);
    }
}

public static GameObject activeGameObject { get; set; }

摘要:Returns the active game object. (The one shown in the inspector).

返回当前点击的场景游戏物体或Project预制体;选择多个则返回第一个选择的;未选择相应的则返回null。

using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

public class Test
{
    [MenuItem(@"Selection/Test")]
    public static void MyTest()
    {
        GameObject obj = Selection.activeGameObject;

        Debug.Log(obj);
    }
}

public static GameObject[] gameObjects { get; }

摘要: Returns the actual game object selection. Includes prefabs, non-modifyable objects. 

返回一个数组,内容为当前点击的场景物体或Project预制体;不符合条件的当前选择不会加入到数组;为选择返回长度为0的数组而不是null。

using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

public class Test
{
    [MenuItem(@"Selection/Test")]
    public static void MyTest()
    {
        GameObject[] objects = Selection.gameObjects;

        foreach (var item in objects)
        {
            Debug.Log(item);
        }
    }
}

public static Transform activeTransform { get; set; }

摘要: Returns the active transform. (The one shown in the inspector).

返回当前点击的场景游戏物体;选择多个则返回第一个选择的;未选择相应的则返回null。

using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

public class Test
{
    [MenuItem(@"Selection/Test")]
    public static void MyTest()
    {
        Transform tra = Selection.activeTransform;

        Debug.Log(tra);
    }
}

public static Transform[] transforms { get; }

摘要:Returns the top level selection, excluding prefabs.

返回一个数组,内容为当前点击的场景物体;不符合条件的当前选择不会加入到数组;为选择返回长度为0的数组而不是null。

using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

public class Test
{
    [MenuItem(@"Selection/Test")]
    public static void MyTest()
    {
        Transform[]transforms = Selection.transforms;

        foreach (var item in transforms)
        {
            Debug.Log(item);
        }
    }
}

public static int[] instanceIDs { get; set; }

摘要:The actual unfiltered selection from the Scene returned as instance ids instead of objects.

返回当前点击的场景物体的实例id而不是对象。

using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

public class Test
{
    [MenuItem(@"Selection/Test")]
    public static void MyTest()
    {
        int[]arr = Selection.instanceIDs;

        foreach (var item in arr)
        {
            Debug.Log(item);
        }
    }
}

public static string[] assetGUIDs { get; }

摘要:Returns the guids of the selected assets.返回所选资产的guid。

using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

public class Test
{
    [MenuItem(@"Selection/Test")]
    public static void MyTest()
    {
        string[]arr = Selection.assetGUIDs;

        foreach (var item in arr)
        {
            Debug.Log(item);
        }
    }
}

方法: 

public static bool Contains(UnityEngine.Object obj);

摘要:Returns whether an object is contained in the current selection.返回当前选择中是否包含指定对象。

using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

public class Test
{
    [MenuItem(@"Selection/Test")]
    public static void MyTest()
    {
        Object[] objs= Selection.gameObjects;

        Debug.Log(Selection.Contains(objs[0]));
    }
}

public static bool Contains(int instanceID);  

摘要:Returns whether an object is contained in the current selection. 返回当前选择中是否包含指定对象。 

using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

public class Test
{
    [MenuItem(@"Selection/Test")]
    public static void MyTest()
    {
        int[] arr= Selection.instanceIDs;

        Debug.Log(Selection.Contains(arr[0]));
    }
}

 public static UnityEngine.Object[] GetFiltered(Type type, SelectionMode mode); 

 摘要:Returns the current selection filtered by type and mode.返回按类型和模式筛选的当前选择。

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

public class Test
{
    [MenuItem(@"Selection/Test")]
    public static void MyTest()
    {
        //获取场景中物体或预制体资源
        UnityEngine.Object[]objs = Selection.GetFiltered(typeof(GameObject),SelectionMode.Assets);

        foreach (var item in objs)
        {
            Debug.Log(item);
        }
    }
}

public static T[] GetFiltered<T>(SelectionMode mode); 

 效果同上

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

public class Test
{
    [MenuItem(@"Selection/Test")]
    public static void MyTest()
    {
        GameObject[]objs = Selection.GetFiltered<GameObject> (SelectionMode.Assets);

        foreach (var item in objs)
        {
            Debug.Log(item);
        }
    }
}

public static Transform[] GetTransforms(SelectionMode mode); 

摘要: Allows for fine grained control of the selection type using the SelectionMode bitmask.

返回按类型和模式筛选的当前选择物体的transform。

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

public class Test
{
    [MenuItem(@"Selection/Test")]
    public static void MyTest()
    {
        Transform[]tras = Selection.GetTransforms(SelectionMode.ExcludePrefab);

        foreach (var item in tras)
        {
            Debug.Log(item);
        }
    }
}

SelectionMode 枚举

 Unfiltered = 0,返回整个选择。

TopLevel = 1,只返回所选的最上面的transform。另一个选定transform的选定子元素将被过滤掉。

Deep = 2,返回选择项和该选择项的所有子物体。

ExcludePrefab = 4,从选择中排除预制体。

Editable = 8,排除任何不可修改的对象。

Assets = 16,只返回资产目录中的资产对象。

DeepAssets = 32 如果选择包含文件夹,还应在文件层次结构中包含该文件夹内的所有资产和子文件夹。

public static void SetActiveObjectWithContext(UnityEngine.Object obj, UnityEngine.Object context);

摘要:Selects an object with a context.选择具有上下文的对象。

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

public class Test
{
    [MenuItem(@"Selection/Test")]
    public static void MyTest()
    {
        UnityEngine.Object obj = new UnityEngine.Object();
        UnityEngine.Object context = new UnityEngine.Object();
        Selection.SetActiveObjectWithContext(obj,context);
    }
}

 

public static Action selectionChanged;

摘要:Delegate callback triggered when currently active/selected item has changed.委托,选择的东西变化的时候调用。

 

 

 

 

 

 

  • 2
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值