Unity 监听Project视图结构变化的事件

今天无意间发现了一个更好的方法来监听Project视图中资源的  创建  删除  移动 保存。把如下脚本放在unity工程中即可,推荐放在Editor目录下。

// Originally written by Lasse Makholm, I believe.
// Changed a bit for non-Team-license systems by Jamie Fristrom ( happionlabs.com / @happionlabs ) - without Team license, it'll post an error message
// if you try to save to a readonly file. With Team license, it will prevent you from editing a readonly file.
 
// I'm pretty sure Unity intended to release this into the wild but not positive.
 
/*
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 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.
*/
 
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using System;
 
public class RespectReadOnly : UnityEditor.AssetModificationProcessor
{
	// Known issues:
	// You can still apply changes to prefabs of locked files (but the prefabs wont be saved)
	// You can add add components to prefabs (but the prefabs wont be saved)
	// IsOpenForEdit might get called a few too many times per object selection, so try and cache the result for performance (i.e called in same frame)
 
	public static void OnWillCreateAsset (string path)
	{
		Debug.Log ("OnWillCreateAsset " + path);
	}
 
	public static string[] OnWillSaveAssets (string[] paths)
	{
		List<string> result = new List<string>();
		foreach( var path in paths )
		{
			if( IsUnlocked(path))
				result.Add ( path );
			else
				Debug.LogError ( path + " is read-only.");
		}
		Debug.Log ("OnWillSaveAssets");
		return result.ToArray();
	}
 
	public static AssetMoveResult OnWillMoveAsset (string oldPath, string newPath)
	{
		AssetMoveResult result = AssetMoveResult.DidNotMove;
		if (IsLocked (oldPath)) {
			Debug.LogError (string.Format ("Could not move {0} to {1} because {0} is locked!", oldPath, newPath));
			result = AssetMoveResult.FailedMove;
		} else if (IsLocked (newPath)) {
			Debug.LogError (string.Format ("Could not move {0} to {1} because {1} is locked!", oldPath, newPath));
			result = AssetMoveResult.FailedMove;
		}
		Debug.Log ("OnWillMoveAsset  from" + oldPath +" to " + newPath);
		return result;
	}
 
	public static AssetDeleteResult OnWillDeleteAsset (string assetPath, RemoveAssetOptions option)
	{
		if (IsLocked (assetPath)) {
			Debug.LogError (string.Format ("Could not delete {0} because it is locked!", assetPath));
			return AssetDeleteResult.FailedDelete;
		}
 
		Debug.Log ("OnWillDeleteAsset" + assetPath);
		return AssetDeleteResult.DidNotDelete;
	}
 
	public static bool IsOpenForEdit (string assetPath, out string message)
	{
		if (IsLocked (assetPath)) {
			message = "File is locked for editing!";
			return false;
		} else {
			message = null;
			return true;
		}
	}
 
	static bool IsUnlocked (string path)
	{
		return !IsLocked (path);
	}
 
	static bool IsLocked (string path)
	{
		if (!File.Exists (path))
			return false;
		FileInfo fi = new FileInfo (path);
		return fi.IsReadOnly;
	}
}

另外注意一下, 此方法是监听将要进行 创建  删除  移动 保存 的操作, 也就是程序到下一帧才会真正执行 创建  删除  移动 保存。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值