获取预制和获取gameObject

本文介绍了如何在Unity中通过菜单项获取预制和项目内的资产,包括使用`Selection.GetFiltered`方法筛选特定类型的对象,并通过遍历过滤后的对象列表来获取所需的资源路径和名称。
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;

public class selectionFn : ScriptableObject {

    [MenuItem("Learn/selection")]
    static void _selection()
    {
        //获取预制的方法
        ArrayList getPrefabs = new ArrayList();
        Object[] objs = Selection.GetFiltered(typeof(Object),SelectionMode.DeepAssets);

        foreach(Object c in objs)
        {
            string _Path =  AssetDatabase.GetAssetPath(c);
            if(_Path.Contains(".prefab"))
            {
                getPrefabs.Add(c);
                //Debug.Log(_Path);
            }
        }
        //Debug.Log(getPrefabs.Count.ToString());

        //2写的获取project里资产的方法
        Object[] _objs = Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.Assets | SelectionMode.DeepAssets);
        foreach(var obj in _objs)
        {
            if (obj is GameObject)
            {
                GameObject go = obj as GameObject;
                Debug.Log("Asset:" + go.name);
            }
        }
    }
}

 

转载于:https://www.cnblogs.com/softimagewht/p/3928831.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值