Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Exclude DLL on specific platform

Discussion in 'Editor & General Support' started by Dreamwriter, Nov 29, 2011.

  1. Dreamwriter

    Dreamwriter

    Joined:
    Jul 22, 2011
    Posts:
    472
    We've got a DLL from a plugin that we need to use in our game, but the DLL won't compile on certain platforms that we build for. We'd like to be able to build for those platforms excluding the DLL, but can't figure out how. Any clue?
     
  2. Wolfos

    Wolfos

    Joined:
    Mar 17, 2011
    Posts:
    950
  3. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    You need to write a little editor script that renames the file to dlx from dll for example, refreshes the asset database, builds and then reverts that.

    we have that in our project to exclude the database libraries from webbuilds for example and have the using XXXX within #if !UNITY_WEBPLAYER blocks
     
  4. Dreamwriter

    Dreamwriter

    Joined:
    Jul 22, 2011
    Posts:
    472
    Dreamora, that sounds like it'd work, though it's a bit of a hack. Wolfos, that wouldn't work, because that requires you have the source code to the DLL so you can add #ifdefs to it.
     
  5. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    Its not a 'would' but a does, we use it it for MySQL :)

    As you will sooner or later add menu entries for building specific clients and versions its a thing you will write anyway, now you just have a reason to do it earlier :)

    we have 2 special folders, OnlyEditor and NotWeb which get accordingly changed upon build to overcome platform limitations and exclude things from builds we don't want to be there (as we have some quite specific editor extensions that do db - photon networking - etc kind of work that is fed into editor windows etc for example which requires them to be in non-editor scripts to work ... we even bridge editor <-> play mode)
     
  6. PrettyFlyGames

    PrettyFlyGames

    Joined:
    Aug 30, 2012
    Posts:
    74
    I need to do what you describe above but not sure where to start. Do you have the possibility to attach your script or make a rough psedo code for it?
     
  7. KyleHatch85

    KyleHatch85

    Joined:
    Dec 13, 2011
    Posts:
    99
    This is something i did for using log4net. Simple and worked.

    Code (csharp):
    1. using UnityEditor;
    2. using UnityEngine;
    3. using System.Collections;
    4. using System.IO;
    5.  
    6. public class BuildForAndroid
    7. {
    8.     private static string m_sDDLLocation = Application.dataPath + "/Core Project/Plugins/log4net.dll";
    9.     private static string m_sDDLLocationNew = Application.dataPath + "/Core Project/Plugins/log4net.dlx";
    10.     private static string m_sBuildPath =  Application.dataPath + "/../Build/AR Pennant.apk";
    11.     private static string[] m_Scenes = { "Assets/Main Project/Scenes/Main.unity" };
    12.  
    13.     [MenuItem("Tools/Build For Android &B")]
    14.     private static void BuildAPK()
    15.     {
    16.         //rename the file by moving it
    17.         File.Move(m_sDDLLocation, m_sDDLLocationNew);
    18.  
    19.         //re-import the database
    20.         AssetDatabase.Refresh();
    21.  
    22.         //build the file
    23.         BuildOptions Options = BuildOptions.AutoRunPlayer;
    24.         BuildPipeline.BuildPlayer(m_Scenes, m_sBuildPath, BuildTarget.Android, Options);
    25.  
    26.         //rename back
    27.         File.Move(m_sDDLLocationNew, m_sDDLLocation);
    28.  
    29.         //re re-import the database
    30.         AssetDatabase.Refresh();
    31.     }
    32. }
    33.  
     
    skullthug likes this.
  8. Ryuuguu

    Ryuuguu

    Joined:
    Apr 14, 2007
    Posts:
    391
    A little modification to handle multiple files in the same directory.

    Code (csharp):
    1. using UnityEditor;
    2. using UnityEngine;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using System.IO;
    6.  
    7. public class BuildPlatform
    8.    
    9. {
    10.     static string m_sDDLLocation = Application.dataPath + "/MySQL Improver/";
    11.  
    12.     static string postfix = "X";
    13.     static string m_sBuildPath =  Application.dataPath + "/../Build/LoopyKong1.apk";
    14.     static List<string> files =new List<string> (){"I18N.dll","I18N.West.dll","Mysql.Data.dll",
    15.         "MySQL.Improver.dll","System.Data.dll"};
    16.     static string[] m_Scenes = { "Assets/A_Trains/scenes/Web.unity" };
    17.  
    18.     [MenuItem("Tools/Build For WebPlayer &B")]
    19.     private static void BuildWeb()  {
    20.         //rename the file by moving it
    21.         files.ForEach(f => {
    22.             File.Move(m_sDDLLocation+f, m_sDDLLocation+f+postfix);
    23.         });
    24.         //re-import the database
    25.         AssetDatabase.Refresh();
    26.  
    27.         //build the file   
    28.         BuildOptions Options = BuildOptions.AutoRunPlayer;
    29.         BuildPipeline.BuildPlayer(m_Scenes, m_sBuildPath, BuildTarget.WebPlayer, Options);
    30.    
    31.         //rename back
    32.         files.ForEach(f => {
    33.             File.Move(m_sDDLLocation+f+postfix, m_sDDLLocation+f);
    34.         });
    35.  
    36.         //re re-import the database
    37.         AssetDatabase.Refresh();   
    38.     }
    39.    
    40. }
     
    skullthug likes this.
  9. jotapeh_

    jotapeh_

    Joined:
    Nov 16, 2012
    Posts:
    45
    This solution works very well for most platforms. What I'm having an issue with at the moment is that even though this works great for Windows Phone, it doesn't work at all for Windows Store. Files that have been moved prior to calling BuildPipeline.BuildPlayer still exist in the Visual Studio project that is compiled.

    Has anyone else seen this behaviour? Does anyone know of a work around?
     
  10. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    5,608
    Unity really needs to add special folders for each platform like they do for Metro. Renaming a DLL through System.IO will not rename the associated meta file (which may or may not be a visible file). This has the side effect of causing Unity to create a new GUID for your DLL every time you rename it, which a rather serious problem for asset store developers since you want your file GUIDs to be static for upgrading purposes, etc. Renaming with AssetDatabase.RenameAsset would be the way to go, except they conveniently decided to prevent you from changing the file extension with that method.

    Anyone know of another way of renaming the file that will keep the meta information intact and allow you to change the extension?
     
  11. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    5,608
    Actually what would be better would be to have flags on each file that specifies which platforms it will be included in.
     
  12. tgraupmann

    tgraupmann

    Joined:
    Sep 14, 2007
    Posts:
    828
    It would be great if Unity had a feature where the folder name would specify the build platforms.

    If I include a DLL it's automatically included for the Editor. But in my case I have code that I don't want to be included in the editor.

    I use precompiler directives like

    #if UNITY_ANDROID && !UNITY_EDITOR

    some code

    #endif

    A nice feature would be a folder structure to describe this.

    ```
    #if UNITY_ANDROID && !UNITY_EDITOR
    ```
    Assets/UnityAndroid/NotUnityEditor/My.DLL

    ```
    #if UNITY_ANDROID && UNITY_EDITOR
    ```
    Assets/UnityAndroid/UnityEditor/My.DLL

    That way I wouldn't have a DLL that's breaking WP8 users.
     
  13. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,645
    There's a feature coming in Unity 5 called Plugin Inspector. It will allow you to select for which platforms which plugins should be used as easily as selecting the appropriate plugin checkboxes :).
     
  14. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    5,608
    Yes, it's very nice in the beta and should solve all the cross platform DLL issues nicely.
     
  15. Frederic Zindagi

    Frederic Zindagi

    Joined:
    Jun 11, 2015
    Posts:
    3
    It seems to only be available for .dll files though; why not any type of file? Or at least folders? They all have a meta file that could store that information.
     
  16. Deepscorn

    Deepscorn

    Joined:
    Aug 13, 2015
    Posts:
    25
  17. Mazyod

    Mazyod

    Joined:
    Apr 21, 2014
    Posts:
    25
    It specifically says "Asset Bundles cannot contain scripts", so it won't work.