0
\$\begingroup\$

I am using this code snippet in unity 2021.3.16 to create Asset bundles:

using System.IO;
using UnityEditor;
using UnityEngine;
using System.Security.Cryptography;
public class ABEncryption 
{

    [MenuItem("ABD/Build AssetBundles")]
    static void ABCreate()
    {
        string _assetNameForAB = "Assets/Prefab/Cube.prefab";
        string _ABName = "MyAB";
        
        AssetBundleBuild[] assetBundleBuilds = new AssetBundleBuild[1];
        assetBundleBuilds[0].assetBundleName = _ABName;
        assetBundleBuilds[0].assetBundleVariant = "";
        assetBundleBuilds[0].assetNames = new string[] { _assetNameForAB };

        BuildAssetBundleOptions buildAssetBundleOptions = BuildAssetBundleOptions.None;
        buildAssetBundleOptions |= BuildAssetBundleOptions.DeterministicAssetBundle;
        buildAssetBundleOptions |= BuildAssetBundleOptions.ChunkBasedCompression;

        string outputPath = Application.streamingAssetsPath  + "/EncryptedAB/";


        BuildPipeline.BuildAssetBundles(outputPath,
                                        assetBundleBuilds,
                                        BuildAssetBundleOptions.None,
                                        BuildTarget.StandaloneWindows64);

    }

}

Something very strange is happening. Compile time is perfectly fine but when I run this I get several errors:

Assets\ABEncryption.cs(27,36): error CS0103: The name 'BuildAssetBundleOptions' does not exist in the current context

Assets\ABEncryption.cs(38,9): error CS0103: The name 'BuildPipeline' does not exist in the current context

Assets\ABEncryption.cs(40,41): error CS0103: The name 'BuildAssetBundleOptions' does not exist in the current context

Assets\ABEncryption.cs(41,41): error CS0103: The name 'BuildTarget' does not exist in the current context

Assets\ABEncryption.cs(54,9): error CS0103: The name 'BuildPipeline' does not exist in the current context

Assets\ABEncryption.cs(55,41): error CS0103: The name 'BuildAssetBundleOptions' does not exist in the current context

Assets\ABEncryption.cs(56,41): error CS0103: The name 'BuildTarget' does not exist in the current context

Error building Player because scripts had compiler errors

I am running this method in the editor (on context menu). Here is the complete details enter image description here

\$\endgroup\$
6
  • 1
    \$\begingroup\$ It sounds like you're trying to compile code that uses editor-only libraries into the executable game that you give to players. That executable doesn't contain the libraries to create asset bundles. Can you explain where this code sits within your folder structure and type hierarchy? Are you deliberately trying to put this code in-game or was that accidental? \$\endgroup\$
    – DMGregory
    Mar 1 at 13:08
  • \$\begingroup\$ I am running this code in the editor. Via context menu! Please check the updated screenshot in the question. The script is present in a script folder. \$\endgroup\$ Mar 2 at 4:50
  • \$\begingroup\$ I even try with the editor scripts. Still the same errors. \$\endgroup\$ Mar 2 at 5:29
  • \$\begingroup\$ Please edit your question to show us a Minimal Complete Verifiable Example, including the complete code file, not just a snippet. \$\endgroup\$
    – DMGregory
    Mar 2 at 5:55
  • \$\begingroup\$ This is the complete script. \$\endgroup\$ Mar 2 at 5:57

1 Answer 1

2
\$\begingroup\$

From the screenshot, ABEncryption seems to be a MonoBehaviour you attach to a GameObject, which means it is runtime code. Runtime code cannot contain references to Editor code.

A quick fix is wrapping all the editor code that is placed in runtime classes between #if UNITY_EDITOR and #endif compiler directives. This instructs compiler to only compile them in the editor, and not for runtime.

A better way would be to extract that Editor code completely to a separate Editor-only class and make that class a part of either an Editor folder or an Editor-only assembly definition.

\$\endgroup\$
4
  • \$\begingroup\$ Now that we can see the full code, we can see it is not a MonoBehaviour. Your suggestion to put it in an Editor folder is still a good fix though. \$\endgroup\$
    – DMGregory
    Mar 2 at 13:35
  • \$\begingroup\$ @DMGregory It is assigned to a GameObject in the screenshot. You can't assign POCO classes to GameObjects. \$\endgroup\$ Mar 2 at 14:39
  • \$\begingroup\$ Ah whoops! @MuhammadFaizanKhan, please ensure the code you post matches the screenshot or vice versa, or you create a very confusing situation for your readers! \$\endgroup\$
    – DMGregory
    Mar 2 at 15:00
  • \$\begingroup\$ @DMGregory Oh yes actually i have make it non-monobhevior script after your commments. \$\endgroup\$ Mar 3 at 4:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .