Hi I'm trying to make BabelJS and React to work from within Unity so I can use new-style JavaScript like Arrow functions () => {}, from within PowerUI. Apparently the Install=Package from within Visual Studio didn't actually allow unity to get the packages, so I manually installed AdvancedStringBuilder, JavaScriptEngineSwitcher.Core, JSPool, Newtonsoft.Json, and React.Core and simply dropped the net45 DLLs in the Unity Assets folder, and there are no compile errors.
However, when I actually want to use BabelJS the way the documentation (for C#) shows:
(at the beginning of the cs file):
using React;
then later when I want to babel-ify the javascript source code for PowerUI to compile (PowerUI/Source/JavaScript/JavaScriptEngine.cs line ~198):
var babel = ReactEnvironment.Current.Babel;
var newSource = babel.Transform(source);
Engine.Execute(newSource); // PowerUI code
I get this error (from PowerUI but the same would apply in any case):
React.TinyIoC.TinyIoCResolutionException: Unable to resolve type: React.IReactEnvironment
at React.TinyIoC.TinyIoCContainer.ResolveInternal (React.TinyIoC.TinyIoCContainer+TypeRegistration registration, React.TinyIoC.NamedParameterOverloads parameters, React.TinyIoC.ResolveOptions options) [0x001ce] in <c8732b4de84a482c9ad31b41d7faf4d8>:0
at React.TinyIoC.TinyIoCContainer.Resolve (System.Type resolveType) [0x00011] in <c8732b4de84a482c9ad31b41d7faf4d8>:0
at React.TinyIoC.TinyIoCContainer.Resolve[ResolveType] () [0x00000] in <c8732b4de84a482c9ad31b41d7faf4d8>:0
at React.ReactEnvironment.get_Current () [0x00005] in <c8732b4de84a482c9ad31b41d7faf4d8>:0
It seems like React isn't working, does anyone know what else I have to do to get this to work?
You cannot use babelJS or react in Unity.
since Unity 2018.2 the only supported language is C#. Qouted from "Programming in Unity"
What programming languages can you use in Unity?
Unity supports C#, an industry-standard language with some similarities to Java or C++.
Prior to Unity 2018.2 a language similair to JavaScript was supported, called UnityScript. However similair it is not Javascript, and not even based on the ECMA standards.
The confusion is understandable though, since even some official Unity resource refer to "UnityScript" as "Js/Javascript", but the two are completely different.
For a more extensive explanation between Javascript and Unityscript see this wiki.unity3d page
Related
I'm trying to inline-bind my functions using this kind of syntax:
onChange = () => {
}
However, my sublime editor isn't correctly highlighting it:
I'm using the Babel package for sublime for syntax highlighting.
Does anyone know how to make it recognize this sort of style?
Check this
View -> Syntax -> Open all with current extension as... -> Babel -> Javascript(Babel).
or
Ctrl - Shift - P, type "Babel" and select Set Syntax: Javascript(Babel)
Source
ST3 relies on language definitions for providing language features such as code folding, syntax highlighting etc. However, with JavaScript, you have many different flavors of the language - like ES5, ES6, JSX etc. To correctly understand and parse each one of them is not easy given ST3's design (using a language definition file which is mostly regex matching).
So depending on what you're looking for, you may want to install JavaScriptNext - ES6 Syntax which helps ST3 better understand the language and its syntax. There are few others like these on marketplace if I'm not mistaken.
Then comes the notion of syntax highlighting - again, without the core editor natively understanding what's JS and what's JS-like out, these plugins are dependent on how good the language definitions are and so, have one or more shortcomings. There are a few options that you can try and see what suits best:
naomi - Enhanced syntax definitions for Sublime Text 3. Supports stage-0 features
babel-sublime - Syntax definitions for ES6 JavaScript with React JSX extensions. But has some issues with arrow functions, see #301
sublime-react - It's actually deprecated in favor of babel-sublime but you may want to check it out.
Whatever you choose, you need to do some due diligence. Check with their issue lists, see if anything stands out to you. Relying on transpilers can only go so far.
As a long time ST3 user, I've constantly found one or more issues. And depending on whether you're working on pure JS, or React, you may have to keep switching or accept some compromises.
Ultimately, I switched to VSCode (tried Atom too) which understand the language and it's flavors natively and provides extensions API that authors can build upon. Consequently, the syntax understanding and highlighting capabilities are far greater than what you can get out of ST3 + Extensions.
The only solution that comes to mind is to create a custom snippet, for Sublime, to "recognize" the arrow function or in general the reduced syntax to declare a function.
Here are two links that could be useful:
https://medium.freecodecamp.org/a-guide-to-preserving-your-wrists-with-sublime-text-snippets-7541662a53f2
and
https://gist.github.com/LeZuse/2324352
or
https://gist.github.com/ZYinMD/860926a178ccd6d107ffe2c6727b5845
I've been taking a look at ReasonML (https://reasonml.github.io/) and in general, as a 'loyal' ;) functional programmer, I like the idea. However, I believe there's a missing part in my reasoning about the project.
In particular I'm a bit confused if it comes what to search for. For example, I'd like to build a simple web server. Shall I use JS-related libraries (express, ...), OCaml technologies, or maybe yet something else ?
What I'm actually missing is a step-by-step guide that presents a way to build a full basic application (in this case: let's say a simple web server with db connection).
Last thing - forgive me imprecise language. As I said: I'm pretty sure there's a gap in my reasoning about ReasonML and I'd like to fill it ;).
If you want to write portable code, you should use the OCaml technologies, so Array.length (from OCaml core) instead of Js.Array.length (Bucklescript JS wrapper).
If you do not care about native code, but just want to target JS (node/browser), then you can use the FFI and leverage your existing knowledge of JS libraries.
IMHO, this FFI is one of the nicer things of reasonML. The resulting code is small and you can inspect the .bs.js files to see what it's doing.
But as said, you lose the ability to generate native code this way.
Here is an example,
https://github.com/wires/reason-ffi
Say I don't have a range function in OCaml or ReasonML and I don't want to write one, but I know ramda has one. Just write some JS,
// range.js
exports.range = require('ramda').range
Then wrap it with types, like
[#bs.module "./range.js"] external range' : (int,int) => array(int) = "range";
let range : (int,int) => list(int) = (a,b) => range'(a,b) |> Array.to_list
I'm not saying this is the ultimate way to use this tool, but I find it a very frictionless way to transition untyped garbage JS to something reasonably maintainable.
And you can leverage your existing JS library knowledge and keep building with reasonML, instead of spending your time writing a boring range function (which is also learning... of course)
Basically, I remember that there was a time when you could use the following: <?Mapping ... > (It doesn't seem to work anymore -- Deprecated perhaps?)
I know I can map my XML namespaces like so...
xmlns:xyzcon="clr-namespace:XYZ.Wpf.Controls"
xmlns:xyzcom="clr-namespace:XYZ.Wpf.Commands"
But, I also know that I can map my XML namespaces like so (much cleaner)
[assembly: XmlnsPrefix("http://schemas.mycompany.com/netfx/xaml/presentation", "xyz")]
[assembly: XmlnsDefinition("http://schemas.mycompany.com/netfx/xaml/presentation", "XYZ.Wpf.Controls")]
[assembly: XmlnsDefinition("http://schemas.mycompany.com/netfx/xaml/presentation", "XYZ.Wpf.Commands")]
which makes it very easy to use because instead having the l, lv, etc. prefix I can have all my CLR-Namespaces point to the same XML namespace...
xmlns:xyz="http://schemas.mycompany.com/netfx/xaml/presentation"
That line gives me access to XYZ.Wpf.Controls and XYZ.Wpf.Commands. The problem is that this only works if the assembly is already compiled. I can't use this trick when working within the same assembly.
Basically, from within the same app or assembly, I want to map different CLR namespaces to the SAME XML namespace on a global scale (that way throughout my app I simply have to include that one XML namespace). Any way to do that?
It appears that there is still no way to map local namespaces within a WPF project. The book Programming WPF: Building Windows UI with Windows Presentation Foundation (published in 2007) states:
WPF projects in Visual Studio cannot use namespaces introduced by XmlnsDefinitionAttribute from XAML that live in the same assembly. If you want to refer to locally defined types, you must use the alternative mechanism...
The "alternative mechanism" being the dreaded xmlns:local="..." for each namespace.
My understanding is that the executing assembly (in this case the local WPF assembly) searches all referenced assemblies for both XmlnsPrefixAttribute and XmlnsDefinitionAttribute but does not search itself. It makes no sense that 4 years later this behavior has not changed, but that appears to be the case.
I'm currently using a lot of the same subclassed objects with custom methods. It would be more convenient to create my own library which I can use for several projects.
The goal is to have my own classes being available in the same way classes like UIView, CGRect etc are, including convenient methods like CGRectMake, both with classes and structs. To sum it up, I want to create my own equivalents of:
Classes like UIView
Structs like CGRect
Convenient functions like CGRectMake
Have this available as a library
Have this available as an XCode template, thus, having these custom Objects available as 'new files' in XCode
So basically I'm looking for instructions on how to create classes, structs etc in order to create all the above. What is the best way to do this? The 320 project seems like a good starting point. But it lacks (I think) in:
having the library available in new projects right away
having the new classes available under 'new file'
Even if I would create an own static library, will I be able to release the app on the app store, since linking to 3rd party libraries is not supported on the phone?
For your convenience, these are basically the sub questions, covering the scope of this question:
How can I create my own library for Mac / iPhone development?
How do I create classes, structs and inline function for this library?
How do I create my own Xcode template based on this library?
Will I be able to release iPhone apps using my own static library?
FYI Xcode 3.2 has a new project template called Cocoa Touch Static Library. You might want to go that route.
If you were doing this for a Mac, you'd create a framework. However, you mention UIView, so obviously you're working with the iPhone. Apple doesn't allow iPhone applications to dynamically link against other libraries at runtime, so your only option is to create a static library. A static library is linked into the application executable when it's built.
To my knowledge, there's no static library project template in Xcode. What you'll likely have to do is start with a different iPhone Xcode template and add a Static Library target. Hang on to the default application target; you can use that to build a simple test application to make sure the library actually works.
To actually use the library in an application, you'll need two things: the compiled library (it has a .a extension) and all the header files. In your finished application, you'll link against your static library, and you'll need to #import the header files so that the compiler understands what classes, functions, etc. are available to it. (A common technique is to create one header file that imports all the others. That way, you only need to import a single file in your source files.)
As for creating your own custom templates, there's a simple tutorial here that should get you started: http://www.macresearch.org/custom_xcode_templates You can probably copy the default templates and just customize them to suit your purposes.
The struct syntax looks like this:
typedef struct _MyPoint {
CGFloat x;
CGFloat y;
} MyPoint;
Structs are are declared in header files, so you can (I believe) Command+Double Click on the name of a struct to see how it's declared.
Another little trick for creating structs is to do something like this:
MyPoint aPoint = (MyPoint){ 1.5f, 0.25f };
Because the compiler knows the order of fields in the struct, it can very easily match up the values you provide in the curly braces to the appropriate fields. Of course, it's more convenient to have a function like MyPointMake, so you can write that like this:
MyPoint MyPointMake(CGFloat x, CGFloat y)
return (MyPoint){ x, y };
}
Note that this is a function, not a method, so it lives outside of any #interface or #implementation context. Although I don't think the compiler complains if you define it in an #implementation context.
CGPointMake is defined as what's known as an inline function. You can see the definition for that in the Cocoa header files, too. (The difference between an inline function and a normal function is that the compiler can replace a call to CGPointMake with a copy of CGPointMake, which avoids the overhead of making a function call. It's a pretty minor optimization, but for a function that simple, it makes sense.)
The 320 project is a good example of an iPhone class library. You basically compile your project down into a .a library and then statically link against this in your client projects.
Since this is a community wiki now, I thought it will be helpful to link some resources and tutorials:
http://blog.stormyprods.com/2008/11/using-static-libraries-with-iphone-sdk.html
http://www.clintharris.net/2009/iphone-app-shared-libraries/
Enjoy!
The 320 project seems like a good starting point indeed. But it lacks (I think) in:
having the library available in new projects right away
having the new classes available under 'new file'
Those are project and file templates. For more information, ask the Google.
If you plan on releasing this on the app store, you wont be able to use your library in the way that you would like. As mentioned above, linking to 3rd party libraries is not supported on the phone. I think there is a 'hack' way to make it work, but you'll lose distribution.
The best I could come up with was putting all the relevant code in a directory and sharing it that way. I know its not as elegant, but its their limitation ie. out of our control.
I am trying to add Silverlight support to my favorite programming langauge Nemerle.
Nemerle , on compilation procedure, loads all types via reflection mainly in 2 steps
1-) Uses Assembly.LoadFrom to load assembly
2-) Usese Assembly.GetTypes() to get the types
Then at the end of compilation it emits the resolved types with Reflection.Emit.
This procedure works for all assemblies including Silverlight ones except mscorlib of silverlight.
In c# this fails:
var a = System.Reflection.Assembly.LoadFrom(#"c:\mscorlib.dll");
but this passes :
var a = System.Reflection.Assembly.ReflectionOnlyLoadFrom(#"c:\mscorlib.dll");
Bu in the latter , a.GetTypes() throws an exception sayin System.Object's parent does not exist.
Is there a way out ?
Assuming you're trying to reflect over Silverlight's mscorlib from the standard CLR, this won't work because the CLR doesn't permit loading multiple versions of mscorlib. (Perhaps this is because it could upset resolution of its core types).
A workaround is to use Mono.Cecil to inspect the types:
http://mono-project.com/Cecil. This library actually performs better than .NET's Reflection and is supposed to be more powerful.
Here's some code to get you started:
AssemblyDefinition asm = AssemblyFactory.GetAssembly(#"C:\mscorlib.dll");
var types =
from ModuleDefinition m in asm.Modules
from TypeDefinition t in m.Types
select t.Name;
You can compile Nemerle with Silverlight assembly and then you have Nemerle working on top of Silverlight :)