puerts icon indicating copy to clipboard operation
puerts copied to clipboard

[Unity] ts中方法调用和c# 中不一致

Open lcals opened this issue 5 years ago • 8 comments

调用代码 c#: image 表现 image 调用代码 ts: image 表现 image

理应无限执行 实际不是

aAssetLoad对象 为抽象类

public AAssetLoad LoadSceneAsync(string assetBundleName, string sceneName, bool isAdditive,
         BuildType buildType)
     {
         AAssetLoad  operation = new AssetSceneEditorSimulation(assetBundleName, sceneName, isAdditive);
   

         return operation;
     }
public  abstract class AAssetLoad : IEnumerator
 {
         public virtual float Progress => 0.0f;

 } 
public class AssetSceneEditorSimulation : AAssetLoad
 {  
     private readonly AsyncOperation _operation;
    public override float Progress => _operation?.progress ?? 0.0f;
     public AssetSceneEditorSimulation(string assetBundleName, string sceneName, bool isAdditive)
     {
         var levelPaths =
             UnityEditor.AssetDatabase.GetAssetPathsFromAssetBundleAndAssetName(assetBundleName, sceneName);
         if (levelPaths.Length == 0)
         {
             Debug.LogErrorFormat("[AssetLoadManager]:There is no scene with name : {0} in {1}", sceneName,
                 assetBundleName);
             return;
         }


         if (isAdditive)
         {
             var param = new LoadSceneParameters(LoadSceneMode.Additive, LocalPhysicsMode.Physics3D);
             _operation =
                 UnityEditor.SceneManagement.EditorSceneManager.LoadSceneAsyncInPlayMode(levelPaths[0], param);
         }
         else
         {
             var param = new LoadSceneParameters(LoadSceneMode.Single, LocalPhysicsMode.Physics3D);
             _operation =
                 UnityEditor.SceneManagement.EditorSceneManager.LoadSceneAsyncInPlayMode(levelPaths[0], param);
         }
     }
 }

lcals avatar Oct 16 '21 04:10 lcals

你是不是没有在 Update() 里调用 jsEnv.Tick();

PleijaNet avatar Oct 16 '21 04:10 PleijaNet

调用了

lcals avatar Oct 16 '21 04:10 lcals

实验 API SceneManager.LoadSceneAsync C# TS 表现正常 实验 API UnityEditor.SceneManagement.EditorSceneManager.LoadSceneAsyncInPlayMode C# 表现正常 TS不正常

lcals avatar Oct 16 '21 05:10 lcals

editor的你要确保你的tick()正常执行, EditorApplication.OnUpate(大概是这个名字)可以在editor未运行状态执行update, 这个方法只要你第一次有输出它就是正常的, 后续没有输出的原因只有一种, 因为你的Tick()没有重复执行, 你可以进行简单的测试, 就是在你调用jsEnv.Tick()的时候, 不是调用一次, 你可以再 sleep() 一段时间, 然后在手动调用一次jsEnv.Tick(); 如果你调用两次就能看到两个输出, 调用3次看到三次输出, 这纯粹就是tick的问题, 跟cs/ts行为没啥关系

PleijaNet avatar Oct 16 '21 05:10 PleijaNet

EditorApplication.update +=  () => jsEnv.Tick();

PleijaNet avatar Oct 16 '21 05:10 PleijaNet

其实只要你第一条ts的调用没有报错, 那它就是正常的.. 不正常的话, 第一条语句就应该报错了, 这只是普通的Api调用

PleijaNet avatar Oct 16 '21 05:10 PleijaNet

你这测试写得不理想, 应该在setInterval()里面的调用加入 console.log 检查实际执行的次数, 你这样测试都搞不清楚它有没有执行还是执行了但结果异常

PleijaNet avatar Oct 16 '21 05:10 PleijaNet

EditorApplication.update += () => jsEnv.Tick(); 添加类似的逻辑就好 谢谢 ^_^

lcals avatar Oct 16 '21 06:10 lcals