今天雨松MOMO无意间发现了一个更好的方法来监听Project视图中资源的 创建 删除 移动 保存。把如下脚本放在unity工程中即可,推荐放在Editor目录下。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
// 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
- 本文固定链接: https://www.xuanyusong.com/archives/3535
- 转载请注明: 雨松MOMO 于 雨松MOMO程序研究院 发表
捐 赠写博客不易,如果您想请我喝一杯星巴克的话?就进来看吧!
OnWillSaveAssets这段代码会导致对文件重命名功能失效,会报错。
而且,对移动和删除的返回值进行修改,也不能阻止这些操作
请问,这些问题,要怎么解决呢?
具体用处是什么呢,比如导入图片自动压缩格式?
这是是一种用法,我想做的是监听删除 更改事件。来同步更新 prefab 里的 prefab 这种情况。