/// <summary>
        /// Prefab の diff データを作成
        /// </summary>
        internal static PrefabDiff GetDiff(string path)
        {
            // Unity内のパスへ変換
            var assetPath = path;

            if (!assetPath.StartsWith("Assets/"))
            {
                var index = path.IndexOf("Assets/");
                assetPath = path.Substring(index);
            }

            var diff = Command.Exec($"git diff \'{assetPath}\'");

            // 差分無し
            if (string.IsNullOrEmpty(diff))
            {
                Debug.Log($"{assetPath} is not Update");
                return(null);
            }

            var target = AssetDatabase.LoadAssetAtPath <GameObject>(assetPath);

            if (target == null)
            {
                Debug.Log($"{assetPath} is illegal");
                return(null);
            }

            var fileName  = Path.GetFileNameWithoutExtension(assetPath);
            var extension = Path.GetExtension(assetPath);
            var dirName   = Path.GetDirectoryName(assetPath);
            var tempPath  = Path.Combine(dirName, fileName + "_temp" + extension);

            // 修正前のデータを取得
            // ここはunity からのパスじゃなくてフルパスが必要
            var yaml = Command.Exec($"git show \'HEAD:{path}\'");

            if (string.IsNullOrEmpty(yaml))
            {
                // 一応両方で確認
                yaml = Command.Exec($"git show \'HEAD:{assetPath}\'");
                if (string.IsNullOrEmpty(yaml))
                {
                    Debug.Log($"{assetPath} prev yaml is empty");
                    return(null);
                }
            }

            File.WriteAllText(tempPath, yaml);
            AssetDatabase.Refresh();

            PrefabDiff diffData   = null;
            var        tempPrefab = AssetDatabase.LoadAssetAtPath <GameObject>(tempPath);

            if (tempPrefab != null)
            {
                // エラーになると作成した Prefab 消せないので
                try
                {
                    var info = Recursive(target.transform, TargetFlag.Add);
                    // 混ぜる
                    AddRecursive(tempPrefab.transform, info);

                    var currentYaml = PrefabYamlUtil.Parse(assetPath);
                    var prevYaml    = PrefabYamlUtil.Parse(tempPath);
                    CheckFieldDiff(info, currentYaml, prevYaml);

                    diffData = new PrefabDiff(info);
                }
                catch
                {
                    // ignored
                }
            }

            AssetDatabase.DeleteAsset(tempPath);
            AssetDatabase.Refresh();

            return(diffData);
        }
Exemple #2
0
 internal void SetDiff(PrefabDiff diff)
 {
     _diff = diff;
     Reload();
     ExpandAll();
 }