首页 >Unity3D频道 >【Unity杂文】 > Unity3D研究院之监听Project视图结构变化的事件
2015
06-15

Unity3D研究院之监听Project视图结构变化的事件

今天雨松MOMO无意间发现了一个更好的方法来监听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;
	}
}

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

原文在这里 也可以直接下载。http://gamedevblog.typepad.com/RespectReadOnly.cs

 

最后编辑:
作者:雨松MOMO
专注移动互联网,Unity3D游戏开发
捐 赠写博客不易,如果您想请我喝一杯星巴克的话?就进来看吧!

Unity3D研究院之监听Project视图结构变化的事件》有 6 条评论

  1. 东东说:

    OnWillSaveAssets这段代码会导致对文件重命名功能失效,会报错。

  2. darling说:

    具体用处是什么呢,比如导入图片自动压缩格式? Unity3D研究院之监听Project视图结构变化的事件 - 雨松MOMO程序研究院 - 1

留下一个回复

你的email不会被公开。 必填项已用 * 标注。