Unity: IPreprocessBuildWithReport is not running on Build - c#

I am a new with Unity and I have been trying to build a Unity project where when it is built, it can also copy some files with the game.exe in the Build folder. For that, I need to understand how IPreprocessBuildWithReport works.
I have created a C# script called MyCustomBuildProcessor.cs and put it inside: Assets\Scripts\Editor The script is as follows:
//Implement this function to receive a callback before the build is started.
using UnityEditor;
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
using UnityEngine;
class MyCustomBuildProcessor : IPreprocessBuildWithReport
{
public int callbackOrder { get { return 0; } }
public void OnPreprocessBuild(BuildReport report)
{
Debug.Log("MyCustomBuildProcessor.OnPreprocessBuild for target " + report.summary.platform + " at path " + report.summary.outputPath);
}
}
To my understanding, this script should run when I build the project, but the Log is not shown in the console. Can someone explain to me what I am doing wrong?
My build setting is as follows:
Unity Build Settings Image , Unity Project Files
My Unity version is: 2022.1.20f1
I have tried building in different version like 2021.3.10f1 and tried placing the file in outside the Editor folder but nothing has worked. Maybe there is something basic that I am missing. I am new to the Unity scene so I have no idea what I am doing wrong.

Related

My visual studio script is not working in Unity

I just started game development and got into a problem
whenever I load my Visual Studio code into Unity it says
The refrenced script unknown on this behaviour is missing
This is my script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class VirtualManMovement : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
Debug.Log("Hello, World!") ;
}
// Update is called once per frame
void Update()
{
}
}
I need a solution for this
This may happen is the file name does not match the class name. Make sure that the file containing this script is also named VirtualManMovement.cs
Make sure in Unity you go to edit -> preferences and see that you have Unity External tools in Your IDE Selected.
Ensure your Code File Name is the same as your Class to prevent confusion.
I was confused as well when I first started. Hopefully, you find this useful

Unity- vs code error: if ($?) { dotnet run $Player_Movement } (Solved)

Hi I am getting "cd "c:\Users\UMUT\Desktop\Unity Projects\Newest\Assets" ; if
($?) { dotnet run $Player_Movement }" message and I get this error message "Couldn't find a project to run. Ensure a project exists in C:\Users\UMUT\Desktop\Unity Projects\Newest\Assets, or pass the path to the project using --project." in my Vs code Unity project. İn my another old project I have a C# script named Player_Movement but I am getting this error in my new Unity Project when I try to debug the project.I downloaded C# extension.I downl Any solutions or reason why it is happening?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class New : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
Debug.Log("hi");
}
// Update is called once per frame
void Update()
{
}
}
I resetted Vs code settings and change my C# extension to older version and error fixed.
I reseted Vs code settings to default and my problem is fixed.

Unity command-line build requires UnityEditor.BuildPipeline, but builds fail for missing 'UnityEditor' reference

I am presently trying to create a batch file for a command-line build of a simple Unity project.
Current environment is:
* Windows 10
* Unity 2017.4.19f1 (Personal)
* Offline network
The project consists of just a simple scene that works fine in the UnityEditor. After failing to get a good build, I've stripped down my build script and batch file to the following:
BareBoneBuild.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using UnityEditor;
namespace Assets.BuildScripts
{
class BareBoneBuild
{
private static string windowsBuildFolderPath = "G:/dev/projects/SimpleProject";
static void PerformBuild()
{
string[] scenes = { "Assets/Scenes/DetectionTest.cs" };
BuildPipeline.BuildPlayer(scenes, windowsBuildFolderPath + "/junk.exe", BuildTarget.StandaloneWindows, BuildOptions.None);
}
}
}
BareBoneBuild.bat
title Unity Build
echo Launching Unity Build...
"C:\Program Files\Unity\Editor\Unity.exe" ^
-batchmode ^
-projectPath G:\dev\projects\SimpleProject ^
-executeMethod Assets.BuildScripts.BareBoneBuild.PerformBuild ^
-quit ^
-logFile G:\dev\projects\SimpleProject\logs\BareBoneBuild.log
echo Build Finished!
PAUSE
BareBoneBuild.log (snippet)
...
-----CompilerOutput:-stdout--exitcode: 1--compilationhadfailure: True--outfile: Temp/Assembly-CSharp.dll
Compilation failed: 1 error(s), 0 warnings
-----CompilerOutput:-stderr----------
Assets/BuildScripts/BareBoneBuild.cs(6,7): error CS0246: The type or namespace name `UnityEditor' could not be found. Are you missing an assembly reference?
-----EndCompilerOutput---------------
- Finished compile Library/ScriptAssemblies/Assembly-CSharp.dll
Assets/BuildScripts/BareBoneBuild.cs(6,7): error CS0246: The type or namespace name `UnityEditor' could not be found. Are you missing an assembly reference?**
(Filename: Assets/BuildScripts/BareBoneBuild.cs Line: 6)
DisplayProgressNotification: Build Failed
Error building Player because scripts had compiler errors
(Filename: Line: -1)
...
EVERY tutorial, blog, youtube video, forum
* https://docs.unity3d.com/2017.4/Documentation/Manual/CommandLineArguments.html
* http://jonathanpeppers.com/Blog/automating-unity3d-builds-with-fake
* https://www.youtube.com/watch?v=4J3SmhGxO1Y
* others...that I don't have the links to
I've viewed states that BuildPipeline.BuildPlayer is the way to build a project from the command-line. BuildPipeline is inside the UnityEditor library.
What am I doing wrong? I'm still new to Unity, so any obvious solution is probably correct.
Thanks in advance for any guidance!
You can't have using UnityEditor; in a build. It is stripped of.
You only need this available in an editor script and not in a build so:
Either use #if pre-processors wrapping everything using it like
#if UNITY_EDITOR
using UnityEditor;
#endif
...
#if UNITY_EDITOR
// anything using UnityEidtor e.g. the method or entire class
#endif
or in your case simply move the entire script into a folder called Editor which strippes of the entire script for a build (see Special folder names).
This is actually also mentioned - though not well explained - by your link:
Then you would need a class in your Unity project in an Editor folder such as this Android example:
or in newer Unity versions you can put it in a separate folder (with any name) and create an AssemblyDefinition and configure it so it is only compiled for the UnityEditor (see in particular Creating an assembly for Editor code)

Test classes do not see the classes under test when unit testing for Unity3D [duplicate]

This question already has answers here:
How to set up unit tests in Unity and fix missing assembly reference error?
(3 answers)
Closed 3 years ago.
I am trying to write unit tests in C# for the scripts that need to run inside Unity3D, later. My C# scripts are in the folder "Assets/Scripts" of the Unity project.
I am using Unity version 2019.1.0f2 with Visual Studio Community for Mac V8.0.5 (build 9), under Mac OS X version 10.14.4.
Unity has a test runner window that I used: Window > General > Test runner. Inside that test runner window, I clicked on "Play mode" and on "Create Playmode Test Assembly Folder". Unity created a folder called "Tests" for me in which I am supposed to put my tests. So far, so good.
Now here is the problem:
My test code cannot see the classes under test that are in the parent folder of that test folder!
This is the folder hierarchy:
Assets
Scripts
MyFirstScript.cs
Tests
MyTestScript.cs
Tests.asmdef
When I double-click on MyTestScript.cs, Visual Studio opens the solution and the source file OK. Now, I am trying to write
var x = new MyFirstScript();
and the compiler complains "The type or namespace name 'MyFirstScript' could not be found".
It seems as if the classes in the Scripts folder are completely unknown inside the classes that are in the Tests folder.
This is MyFirstScript.cs:
using UnityEngine;
public class MyFirstScript : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
And this is MyTestScript.cs:
using System.Collections;
using System.Collections.Generic;
using NUnit.Framework;
using UnityEngine;
using UnityEngine.TestTools;
namespace Tests
{
public class MyTestScript
{
// A Test behaves as an ordinary method
[Test]
public void MyTestScriptSimplePasses()
{
var x = new MyFirstScript(); // problem occurs HERE!
// Use the Assert class to test conditions
}
// A UnityTest behaves like a coroutine in Play Mode. In Edit Mode you can use
// `yield return null;` to skip a frame.
[UnityTest]
public IEnumerator MyTestScriptWithEnumeratorPasses()
{
// Use the Assert class to test conditions.
// Use yield to skip a frame.
yield return null;
}
}
}
I expected that I could use the class MyFirstScript in my test code (MyTestScript). What can I do?
Check out the MonoBehaviourTest section of this manual page.
Note that with Unity, it's almost never a good idea to call 'new' on a MonoBehaviour-derived object. You almost always want to instantiate it by calling AddComponent() on a GameObject.

Calling C# Unity Script from Android

I have been trying to communicate with my Unity C# script through my Android Application using
"UnityPlayer.UnitySendMessage("Cube", "Test","HELLO") - where "Cube" is my Unity Object, "Test" is the name of the method present in the C# script it is using and "HELLO" is the message String I want to pass.
This line is placed in my onClick function, like this:
ImageButton right_button = (ImageButton) findViewById(R.id.right_arrow);
right_button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
UnityPlayer.UnitySendMessage("Cube", "Test","HELLO");
}
});
But when I run my android application and as soon as I click this button, I get this error:
09-11 14:22:37.526: E/AndroidRuntime(1330): java.lang.NoClassDefFoundError: com.unity3d.player.UnityPlayer
I have included classes.jar in my build path also.
Is there anything else I am missing out?
Thanks in advance!
Check if the class.jar file is in the Build Path of your Project. And if so, check if it is selected (check box is marked).
Tip: if you get ClassDefNotFound runtime errors (or similar), you might need to perform this extra step:
go to Project > Properties > Java Build Path -> Order and Export tab and check (tick) the classes.jar, Android x.y and Android Dependencies items; then Apply and rebuild the probject.
Happened to me the other time too.
I clean, re-link and rebuild my eclipse project. Re-Export my jar file to unity and also restart Unity. Build again and the error is gone.

Categories