专注Unity和CPP的小菜鸡 2023-09-23 22:33 采纳率: 47.1%
浏览 10

unity自动添加头注释代码后无法创建新脚本

unity自动添加头注释代码后无法创建新脚本
我在Editor文件夹下创建了脚本,内容如下:

using UnityEngine;
using UnityEditor;    // 被继承的类所在的命名空间
using System.IO;    // IO文件操作命名空间
using System;        // C#基础功能命名空间
using System.Text.RegularExpressions;    // 正则表达式的命名空间

public class SCHead : UnityEditor.AssetModificationProcessor
{
    private static void OnWillCreateAsset(string path)
    {
        path = path.Replace(".meta", "");   // 这里跌path是你的项目主路径Asset/Scripts/文件名
        if (path.EndsWith(".cs"))    // 判断是否是c#文件
        {
            string fileName = Regex.Match(path, @"[^/]*$").Value;    // 通过正则拿到仅含文件名的字符串
            string str = File.ReadAllText(path);    // 获取创建的文件名的全部内容
            str = str.Replace("#Name#", "yyy").Replace("#CreateTime#", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")).Replace("#FileName#", fileName).Replace("#path#", path);    // 将头部注释替换
            File.WriteAllText(path, str);   // 将替换后的内容写入文件,将原内容覆盖
            AssetDatabase.Refresh();
        }
    }
}


然后再创建新脚本时,就不能再创建了,提示报错如下,请问如何解决:

FileNotFoundException: Could not find file "E:\UnityFiles\MeshTest\Assets\Scripts\NewBehaviourScript.cs"
System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.Boolean anonymous, System.IO.FileOptions options) (at <6aa56e57ab504395b555cf3ed50fa53d>:0)
System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.IO.FileOptions options) (at <6aa56e57ab504395b555cf3ed50fa53d>:0)
(wrapper remoting-invoke-with-check) System.IO.FileStream..ctor(string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare,int,System.IO.FileOptions)
System.IO.StreamReader..ctor (System.String path, System.Text.Encoding encoding, System.Boolean detectEncodingFromByteOrderMarks, System.Int32 bufferSize) (at <6aa56e57ab504395b555cf3ed50fa53d>:0)
System.IO.StreamReader..ctor (System.String path, System.Text.Encoding encoding, System.Boolean detectEncodingFromByteOrderMarks) (at <6aa56e57ab504395b555cf3ed50fa53d>:0)
(wrapper remoting-invoke-with-check) System.IO.StreamReader..ctor(string,System.Text.Encoding,bool)
System.IO.File.InternalReadAllText (System.String path, System.Text.Encoding encoding) (at <6aa56e57ab504395b555cf3ed50fa53d>:0)
System.IO.File.ReadAllText (System.String path) (at <6aa56e57ab504395b555cf3ed50fa53d>:0)
SCHead.OnWillCreateAsset (System.String path) (at D:/Program Files (x86)/Unity/MeshTest/Assets/Editor/SCHead.cs:15)
System.Reflection.RuntimeMethodInfo.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at <6aa56e57ab504395b555cf3ed50fa53d>:0)
Rethrow as TargetInvocationException: Exception has been thrown by the target of an invocation.
System.Reflection.RuntimeMethodInfo.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at <6aa56e57ab504395b555cf3ed50fa53d>:0)
System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) (at <6aa56e57ab504395b555cf3ed50fa53d>:0)
UnityEditor.AssetModificationProcessorInternal.OnWillCreateAsset (System.String path) (at <5030de2575e0434f93fa486a5ff56077>:0)
UnityEditor.ProjectWindowUtil.CreateScriptAssetWithContent (System.String pathName, System.String templateContent) (at <5030de2575e0434f93fa486a5ff56077>:0)
UnityEditor.ProjectWindowUtil.CreateScriptAssetFromTemplate (System.String pathName, System.String resourceFile) (at <5030de2575e0434f93fa486a5ff56077>:0)
UnityEditor.ProjectWindowCallback.DoCreateScriptAsset.Action (System.Int32 instanceId, System.String pathName, System.String resourceFile) (at <5030de2575e0434f93fa486a5ff56077>:0)
UnityEditor.ProjectWindowUtil.EndNameEditAction (UnityEditor.ProjectWindowCallback.EndNameEditAction action, System.Int32 instanceId, System.String pathName, System.String resourceFile, System.Boolean accepted) (at <5030de2575e0434f93fa486a5ff56077>:0)
UnityEditor.CreateAssetUtility.EndNewAssetCreation (System.String name) (at <5030de2575e0434f93fa486a5ff56077>:0)
UnityEditor.AssetsTreeViewGUI.RenameEnded () (at <5030de2575e0434f93fa486a5ff56077>:0)
UnityEditor.IMGUI.Controls.TreeViewGUI.EndRename () (at <5030de2575e0434f93fa486a5ff56077>:0)
UnityEditor.IMGUI.Controls.TreeViewGUI.DoRenameOverlay () (at <5030de2575e0434f93fa486a5ff56077>:0)
UnityEditor.IMGUI.Controls.TreeViewGUI.BeginRowGUI () (at <5030de2575e0434f93fa486a5ff56077>:0)
UnityEditor.AssetsTreeViewGUI.BeginRowGUI () (at <5030de2575e0434f93fa486a5ff56077>:0)
UnityEditor.IMGUI.Controls.TreeViewController.OnGUI (UnityEngine.Rect rect, System.Int32 keyboardControlID) (at <5030de2575e0434f93fa486a5ff56077>:0)
UnityEditor.ProjectBrowser.OnGUI () (at <5030de2575e0434f93fa486a5ff56077>:0)
UnityEditor.HostView.InvokeOnGUI (UnityEngine.Rect onGUIPosition) (at <5030de2575e0434f93fa486a5ff56077>:0)
UnityEditor.DockArea.DrawView (UnityEngine.Rect dockAreaRect) (at <5030de2575e0434f93fa486a5ff56077>:0)
UnityEditor.DockArea.OldOnGUI () (at <5030de2575e0434f93fa486a5ff56077>:0)
UnityEngine.UIElements.IMGUIContainer.DoOnGUI (UnityEngine.Event evt, UnityEngine.Matrix4x4 parentTransform, UnityEngine.Rect clippingRect, System.Boolean isComputingLayout, UnityEngine.Rect layoutSize, System.Action onGUIHandler, System.Boolean canAffectFocus) (at <119b9666b32b4bd996496a0c8d0e0595>:0)
UnityEngine.UIElements.IMGUIContainer.HandleIMGUIEvent (UnityEngine.Event e, UnityEngine.Matrix4x4 worldTransform, UnityEngine.Rect clippingRect, System.Action onGUIHandler, System.Boolean canAffectFocus) (at <119b9666b32b4bd996496a0c8d0e0595>:0)
UnityEngine.UIElements.IMGUIContainer.HandleIMGUIEvent (UnityEngine.Event e, System.Action onGUIHandler, System.Boolean canAffectFocus) (at <119b9666b32b4bd996496a0c8d0e0595>:0)
UnityEngine.UIElements.IMGUIContainer.HandleIMGUIEvent (UnityEngine.Event e, System.Boolean canAffectFocus) (at <119b9666b32b4bd996496a0c8d0e0595>:0)
UnityEngine.UIElements.IMGUIContainer.SendEventToIMGUIRaw (UnityEngine.UIElements.EventBase evt, System.Boolean canAffectFocus, System.Boolean verifyBounds) (at <119b9666b32b4bd996496a0c8d0e0595>:0)
UnityEngine.UIElements.IMGUIContainer.SendEventToIMGUI (UnityEngine.UIElements.EventBase evt, System.Boolean canAffectFocus, System.Boolean verifyBounds) (at <119b9666b32b4bd996496a0c8d0e0595>:0)
UnityEngine.UIElements.KeyboardEventDispatchingStrategy.DispatchEvent (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.IPanel panel) (at <119b9666b32b4bd996496a0c8d0e0595>:0)
UnityEngine.UIElements.EventDispatcher.ApplyDispatchingStrategies (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.IPanel panel, System.Boolean imguiEventIsInitiallyUsed) (at <119b9666b32b4bd996496a0c8d0e0595>:0)
UnityEngine.UIElements.EventDispatcher.ProcessEvent (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.IPanel panel) (at <119b9666b32b4bd996496a0c8d0e0595>:0)
UnityEngine.UIElements.EventDispatcher.Dispatch (UnityEngine.UIElements.EventBase evt, UnityEngine.UIElements.IPanel panel, UnityEngine.UIElements.DispatchMode dispatchMode) (at <119b9666b32b4bd996496a0c8d0e0595>:0)
UnityEngine.UIElements.BaseVisualElementPanel.SendEvent (UnityEngine.UIElements.EventBase e, UnityEngine.UIElements.DispatchMode dispatchMode) (at <119b9666b32b4bd996496a0c8d0e0595>:0)
UnityEngine.UIElements.UIElementsUtility.DoDispatch (UnityEngine.UIElements.BaseVisualElementPanel panel) (at <119b9666b32b4bd996496a0c8d0e0595>:0)
UnityEngine.UIElements.UIElementsUtility.UnityEngine.UIElements.IUIElementsUtility.ProcessEvent (System.Int32 instanceID, System.IntPtr nativeEventPtr, System.Boolean& eventHandled) (at <119b9666b32b4bd996496a0c8d0e0595>:0)
UnityEngine.UIElements.UIEventRegistration.ProcessEvent (System.Int32 instanceID, System.IntPtr nativeEventPtr) (at <119b9666b32b4bd996496a0c8d0e0595>:0)
UnityEngine.UIElements.UIEventRegistration+<>c.<.cctor>b__1_2 (System.Int32 i, System.IntPtr ptr) (at <119b9666b32b4bd996496a0c8d0e0595>:0)
UnityEngine.GUIUtility.ProcessEvent (System.Int32 instanceID, System.IntPtr nativeEventPtr, System.Boolean& result) (at <b77449c744f04ea08fcc4bb8900de34c>:0)

  • 写回答

1条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2023-09-24 08:32
    关注

    【相关推荐】




    如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^
    评论

报告相同问题?

问题事件

  • 创建了问题 9月23日