【问题标题】:Why does this c# code fail to complie - assetbundles/objects from StreamingMedia为什么此 c# 代码无法编译 - 来自 Streaming Media 的资产包/对象
【发布时间】:2019-08-05 10:39:11
【问题描述】:

作为 Unity C# 的新手,我遵循了这篇文章的指导: Build and load Assetbundles in Unity

我能够成功地将assetBundles 构建到StreamingMedia 文件夹中。但是在加载的后半部分使用此帖子指导

脚本将无法编译并出现以下错误。我确信这是我在 Unity 中相对较新的技能,但我想知道我缺少什么。一旦我可以成功地重新创建它,最终的意图是使用它从assetBundles 加载GameObject。

非常感谢任何指导。

错误 参考“收益回报资产”;行。

错误资产/脚本/loadBundleAsset.cs(22,23):错误 CS1519:无效令牌';'在类、结构或接口成员声明中

赞赏

已尝试遵循上一篇文章的指导。 Build and load Assetbundles in Unity 也一直在阅读关于在 Unity 中使用 EInumerators 和 Coroutine 的文章

这是我解释该帖子时的脚本基础。

IEnumerator LoadAsset(string assetBundleName, string objectNameToLoad)
{
    string filePath = System.IO.Path.Combine(Application.streamingAssetsPath, "AssetBundles");
    filePath = System.IO.Path.Combine(filePath, assetBundleName);

    //Load "animals" AssetBundle
    var assetBundleCreateRequest = AssetBundle.LoadFromFileAsync(filePath);
    yield return assetBundleCreateRequest;

    AssetBundle asseBundle = assetBundleCreateRequest.assetBundle;

    //Load the "dog" Asset (Use Texture2D since it's a Texture. Use GameObject if prefab)
    AssetBundleRequest asset = asseBundle.LoadAssetAsync<Texture2D>(objectNameToLoad);
    yield return asset;

    //Retrieve the object (Use Texture2D since it's a Texture. Use GameObject if prefab)
    Texture2D loadedAsset = asset.asset as Texture2D;

    //Do something with the loaded loadedAsset  object (Load to RawImage for example) 
    image.texture = loadedAsset;
}

脚本无法编译,提示如下错误。当放置在一个完整的脚本中时,我希望编译。

错误资产/脚本/loadBundleAsset.cs(22,23):错误 CS1519:无效令牌';'在类、结构或接口成员声明中

【问题讨论】:

    标签: c# unity3d assetbundle


    【解决方案1】:

    我看不出这段代码有任何错误。假设我没有遗漏任何东西,那就剩下一个选择:编译器解析太混乱了,编译器甚至无法告诉你 哪里 错误了。

    它认为它仍在前面的“类、结构或接口成员声明”中,可能是因为解析“结束”括号时出现了问题。没有完整的代码,很难说。即使有了它,也可能需要一段时间才能发现。

    我能给出的唯一建议是分别尝试代码的每一部分。自己抛出编译器错误的肯定被破坏了。

    【讨论】:

    • 您好 Christopher,感谢您的指导,并将报告我从这个增量过程中发现的内容。
    • 再次您好@Christopher 感谢您推荐这种迭代方法。结果,我能够正确编译脚本。最后一行的错误问题,image.texture = image.
    • 因为不需要 RawImage 声明,将这些行注释掉解决了这个问题。现在,真正的挑战是实现和测试从 AssetBundle 加载资产的过程。您的回复很有帮助,谢谢
    • @muckletone “错误的问题是最后一行,image.texture = image。”与钥匙一样,它始终是您最后看到的地方:)
    猜你喜欢
    • 1970-01-01
    • 2012-01-03
    • 2021-05-13
    • 2014-10-03
    • 1970-01-01
    • 1970-01-01
    • 2010-10-16
    • 2021-06-11
    • 2016-01-08
    相关资源
    最近更新 更多