1. 程式人生 > >unity--監聽Project檢視結構變化的事件

unity--監聽Project檢視結構變化的事件

監聽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;

    }

}